package com.szwl.controller;
import cn.com.crbank.ommo.esUtil.BeanUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.szwl.constant.ResponseCodesEnum;
import com.szwl.exception.BizException;
import com.szwl.feign.bean.OrderFeign;
import com.szwl.feign.bean.SzwlFeign;
import com.szwl.model.bo.JsonMessage;
import com.szwl.model.bo.R;
import com.szwl.model.bo.ResponseModel;
import com.szwl.model.entity.*;
import com.szwl.model.query.TAdminParam;
import com.szwl.model.utils.*;
import com.szwl.service.*;
import com.szwl.service.es.EsTOrderService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.szwl.constant.ResponseCodesEnum.A0002;
import static com.szwl.model.utils.DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss;
/**
*
* 前端控制器
*
*
* @author wuhs
* @since 2022-06-17
*/
@RestController
@RequestMapping("/tOrder")
public class TOrderController {
@Autowired
SzwlFeign szwlFeign;
@Autowired
OrderFeign orderFeign;
@Autowired
TOrderService orderService;
@Autowired
TOrderTaskService orderTaskService;
@Autowired
TJoinpayMchService joinpayMchService;
@Autowired
TShandeMchService shandeMchService;
@Autowired
TEquipmentService equipmentService;
@Autowired
EsTOrderService esTOrderService;
@Autowired
TSzsmWxService szsmWxService;
@ApiOperation(value = "测试feign")
@GetMapping("/testFeign")
public ResponseModel> testFeign(String id) {
return szwlFeign.testGetAdmin(id);
}
/**
* 合并付款接口,根据终端跳转微信或支付宝
* @param req
* @param resp
* @return
* @throws Exception
*/
@ApiOperation(value = "请求在线支付 主扫")
@RequestMapping("/aliPay")
public RedirectView alipayforward(String wx, String zfb, HttpServletRequest req, HttpServletResponse resp) throws Exception {
String userAgent = req.getHeader("User-Agent");
RedirectView redirectTarget = new RedirectView();
redirectTarget.setContextRelative(true);
if (userAgent.contains("MicroMessenger")){ // 微信
redirectTarget.setUrl(wx);
}else if(userAgent.contains("AlipayClient")){ // 支付宝
redirectTarget.setUrl(zfb);
}else{
throw new BizException(ResponseCodesEnum.A0001,"请使用支付宝或微信扫码!");
}
return redirectTarget;
}
/**
* 请求在线支付 主扫 优惠码 盲盒
*
* @param clientId 设备client id
* @param productName 商品名称
* @param frpCode 支付方式 支付宝:ALIPAY_NATIVE , 微信:WEIXIN_NATIVE
* @param pri 有值代表是盲盒
* @return
*/
@ApiOperation(value = "请求在线支付 主扫")
@GetMapping("/uniPay")
public Object uniPay(String code, String clientId, String productName, String frpCode,String pri) {
// IPage> iPage = R.getDataIfSuccess(szwlFeign.testPay("1"));
// productName = "玫瑰精灵";
String productName1 = productName;
TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
if(equipment==null||equipment.getId()==null){
return JsonMessage.error("找不到设备");
}
//验证优惠码 code=null 则不选用优惠码
TPromoCode promoCode = new TPromoCode();
if(StringUtils.isEmpty(code)){
//没有优惠码
}else {
//有优惠码,需要对优惠码进行验证
// IPage pagePromoCode = R.getDataIfSuccess(szwlFeign.getTPromoCode(code));
ResponseModel tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
// TPromoCode tPromoCode = R.getDataIfSuccess(szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()));
promoCode = tPromoCode;
if (tPromoCode == null||tPromoCode.getId()==null) {
//不存在
return JsonMessage.success("1");
}
// List codeList = pagePromoCode.getRecords();
// if (codeList.size() == 0) {
// //不存在
// return JsonMessage.success("1");
// }
// for (TPromoCode cod : codeList) {
// if(!cod.getAdminId().equals("1")){
// String adminId = cod.getAdminId();
// String adminId1 = String.valueOf(equipment.getAdminId());
// if (adminId.equals(adminId1)) {
// promoCode = cod;
// }
// }else{
// promoCode = cod;
// }
// }
if (promoCode.getId() == null) {
//不存在
return JsonMessage.success("1");
}
Date lastUseDate = null;
if (promoCode != null&&promoCode.getId()!=null) {
lastUseDate = promoCode.getLastUseDate();
}
if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
promoCode.setIsUse("2");
szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
//过期
return JsonMessage.success("4");
}
if (promoCode.getIsUse().equals("1")) {
//被使用
return JsonMessage.success("2");
}
if(!promoCode.getAdminId().equals("1")){
if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
} else {
//不是本机
return JsonMessage.success("3");
}
}
if (promoCode.getDiscount() == null) {
//旧优惠码
return JsonMessage.success("0");
}
if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
//0折
return JsonMessage.success("0");
}
}
if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) {
return JsonMessage.error("参数错误");
}
if (equipment == null) {
return JsonMessage.error("找不到设备");
}
Long equipmentId = equipment.getId();
//查找商品
ResponseModel product1 = szwlFeign.getProduct(String.valueOf(equipmentId), productName);
TProduct product = product1.getData();
// TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
if (product == null) {
return JsonMessage.error("找不到商品");
}
BigDecimal price = product.getRmbPrice();
if(StringUtils.isNotEmpty(pri)){
price = new BigDecimal(pri);
}
if(StringUtils.isNotEmpty(code)){
BigDecimal discount = BigDecimal.valueOf(promoCode.getDiscount());
price = product.getRmbPrice().multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
}
if (BigDecimal.ZERO.compareTo(price) >= 0) {
return JsonMessage.error("商品价格异常");
}
String sn = orderService.initSn(equipmentId);
//获取分销 关联设备
ResponseModel proportion1 = szwlFeign.getProportion(String.valueOf(equipmentId));
TProportion proportion = proportion1.getData();
// TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
if (proportion == null&&proportion.getId()==null) {
return JsonMessage.error("设备商家未完成分销设置");
}
TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
if (admin == null) {
return JsonMessage.error("找不到设备商家");
}
Integer type = proportion.getType();
JSONArray altInfo = new JSONArray();
JSONArray altNewInfo = new JSONArray();
BigDecimal adminProportion = null, agencyProportion = null, merchantProportion = null, personageProportion = null, selfProportion = null;
Long agencyId = proportion.getAgencyId();
Long merchantId = proportion.getMerchantId();
Long personageId = proportion.getPersonageId();
String orderType = null;
if (type == null) {
return JsonMessage.error("设备商家未完成分销设置");
}
BigDecimal agencyAmount = null;
BigDecimal merchantAmount = null;
BigDecimal personageAmount = null;
BigDecimal selfAmount = null;
TJoinpayMch agencyMch = null;
TJoinpayMch merchantMch = null;
TJoinpayMch personageMch = null;
TJoinpayMch selfMch = null;
JSONObject agencyJson = null;
JSONObject merchantJson = null;
JSONObject personageJson = null;
JSONObject selfJson = null;
// 砍掉千6手续费
// BigDecimal cutPrice = price.multiply(new BigDecimal(99.4)).divide(new BigDecimal(100));
//取消平台扣手续费
BigDecimal cutPrice = price.multiply(new BigDecimal(100)).divide(new BigDecimal(100));
switch (type) {
case 0:
agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
if (agencyMch == null ) {
return JsonMessage.error("设备商家未注册提现账户");
}
agencyProportion = proportion.getProportion();
adminProportion = proportion.getAdminProportion();
orderType = "1";
// 代理分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("altMchNo", agencyMch.getAltMchNo());
agencyJson.put("altAmount", agencyAmount.toString());
agencyJson.put("isGuar", "12");
altInfo.add(agencyJson);
break;
case 1:
agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
// agencyMch = mchService.getUniqueness(admin.getAgencyId());
selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
if (agencyMch == null ) {
return JsonMessage.error("设备商家未注册提现账户");
}
if (selfMch == null ) {
return JsonMessage.error("设备商家未注册提现账户");
}
agencyProportion = proportion.getAgencyProportion();
adminProportion = proportion.getAdminProportion();
selfProportion = proportion.getProportion();
orderType = "2";
BigDecimal refuseAmount = new BigDecimal(0.00);
if(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null){
// System.out.println("a等于b");
}else {
// 一级分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("altMchNo", agencyMch.getAltMchNo());
agencyJson.put("altAmount", agencyAmount.toString());
agencyJson.put("isGuar", "12");
altInfo.add(agencyJson);
}
// 商家自己分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
merchantJson = new JSONObject();
merchantJson.put("altMchNo", selfMch.getAltMchNo());
merchantJson.put("altAmount", selfAmount.toString());
merchantJson.put("isGuar", "12");
altInfo.add(merchantJson);
break;
case 2:
agencyMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAgencyId())));
merchantMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getMerchantId())));
selfMch = R.getDataIfSuccess(szwlFeign.getMch(String.valueOf(proportion.getAdminId())));
if (agencyMch == null ) {
return JsonMessage.error("设备商家未注册提现账户");
}
if (merchantMch == null ) {
return JsonMessage.error("设备商家未注册提现账户");
}
if (selfMch == null) {
return JsonMessage.error("设备商家未注册提现账户");
}
agencyProportion = proportion.getAgencyProportion();
adminProportion = proportion.getAdminProportion();
merchantProportion = proportion.getMerchantProportion();
personageProportion = proportion.getPersonageProportion();
selfProportion = proportion.getProportion();
orderType = "3";
BigDecimal refuseAmount1 = new BigDecimal(0.00);
if(agencyProportion.compareTo(refuseAmount1) == 0||agencyProportion==null){
// System.out.println("a等于b");
}else {
// 代理分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("altMchNo", agencyMch.getAltMchNo());
agencyJson.put("altAmount", agencyAmount.toString());
agencyJson.put("isGuar", "12");
altInfo.add(agencyJson);
}
if(merchantProportion.compareTo(refuseAmount1) == 0||merchantProportion==null){
// System.out.println("a等于b");
}else {
// 经销商分销获得利润
merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
merchantJson = new JSONObject();
merchantJson.put("altMchNo", merchantMch.getAltMchNo());
merchantJson.put("altAmount", merchantAmount.toString());
merchantJson.put("isGuar", "12");
altInfo.add(merchantJson);
}
// 商家自己分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
merchantJson = new JSONObject();
merchantJson.put("altMchNo", selfMch.getAltMchNo());
merchantJson.put("altAmount", selfAmount.toString());
merchantJson.put("isGuar", "12");
altInfo.add(merchantJson);
break;
}
String orderNo = sn;
BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
String productDesc = "";
String commonParameter = "";
String returnUrl = null;
String notifyUrl = JoinpayConstant.Notify_Url;
String isShowPic = "1";
String openId = null;
String authCode = null;
String appid = null;
String transactionModel = null;
String tradeMerchantNo = admin.getTradeMerchantNo();
String buyerId = null;
String isAlt = "1";
String altType = "11";
String altUrl = null;
BigDecimal marketingAmount = null;
if(StringUtils.isNotEmpty(pri)){
productName ="棉花糖";
productName1 ="棉花糖";
}
TOrder order = new TOrder();
order.setCreateDate(new Date());
order.setModifyDate(new Date());
order.setAdminId(admin.getId());
order.setSn(sn);
order.setProductId(product.getId());
order.setProductNo(product.getNo());
order.setProductName(productName);
order.setPrice(price);
order.setClientId(equipment.getClientId());
order.setEquipmentId(equipmentId);
order.setFrpCode(frpCode);
order.setAltInfo(altInfo.toString());
order.setStatus(0);
order.setType(type);
order.setCompanyType(admin.getCompanyType());
if(type==0){
order.setAdminProportion(adminProportion);
order.setAgencyProportion(agencyProportion);
order.setAgencyId(admin.getId());
}
if(type==1){
order.setAdminProportion(adminProportion);
order.setAgencyProportion(agencyProportion);
order.setAgencyId(agencyId);
order.setMerchantProportion(merchantProportion);
order.setMerchantId(admin.getId());
}
if(type==2){
order.setAdminProportion(adminProportion);
order.setAgencyProportion(agencyProportion);
order.setAgencyId(agencyId);
order.setMerchantProportion(merchantProportion);
order.setMerchantId(merchantId);
order.setPersonageProportion(personageProportion);
order.setPersonageId(admin.getId());
}
String payPlatform = admin.getPayPlatform();
order.setPayPlatform(payPlatform);
if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
order.setIsSettlement("0");
}
orderService.save(order);
Iterator