package com.szwl.controller;
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.toolkit.Wrappers;
import com.google.gson.Gson;
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.constant.JoinpayConstant;
import com.szwl.model.utils.PushUtils;
import com.szwl.service.TEquipmentService;
import com.szwl.service.TOrderService;
import com.szwl.service.TWeixinPayConfigService;
import com.szwl.utils.IDGenerator;
import com.szwl.weixin.refund.model.CreateRequest;
import com.szwl.weixin.util.HttpUtils;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.Refund;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.wechat.pay.java.service.refund.model.Status.PROCESSING;
/**
*
* 前端控制器
*
*
* @author wuhs
* @since 2023-04-08
*/
@RestController
@RequestMapping("/tWeixinPay")
public class TWeixinPayController {
@Autowired
SzwlFeign szwlFeign;
@Autowired
TOrderService orderService;
@Autowired
TWeixinPayConfigService weixinPayConfigService;
@Autowired
TEquipmentService equipmentService;
@ApiOperation(value = "购物车 请求在线支付 ")
@PostMapping("/carsPay")
public Object carsPay(String clientId, @RequestBody Map productNameMap) {
if(productNameMap==null){
return JsonMessage.error("数据出错");
}
StringBuffer productName1 = new StringBuffer();
StringBuffer note = new StringBuffer();
StringBuffer productName = new StringBuffer();
TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
if(equipment==null||equipment.getId()==null){
return JsonMessage.error("找不到设备");
}
BigDecimal price = new BigDecimal("0.00");
// JSONObject mapList = JSON.parseObject(productNameMap);
TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId()));
if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){
//1,type=0,原始设定,没有第二件半价
for (String key : productNameMap.keySet()) {
String entryValue = String.valueOf(productNameMap.get(key));
List value = JSON.parseArray(entryValue, String.class);
List promoCodeList = new ArrayList<>();
//1,校验优惠码
if(value.size()>0){
for(String code:value){
if(!code.equals("0")){
TPromoCode promoCode = new TPromoCode();
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");
}
Date lastUseDate = null;
if (promoCode != 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.getId() == null) {
//不存在
return JsonMessage.success("1");
}
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");
}
promoCodeList.add(promoCode);
}
}
}
String[] productNum = key.split("-");
String productNamea = productNum[0];
String num = productNum[1];
productName.append(productNamea).append(num);
note.append(productNamea).append(num).append(":");
productName1.append(productNamea).append(num);
//确定价格,然后叠加R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
if (product == null) {
return JsonMessage.error("找不到商品");
}
BigDecimal productPrice = product.getRmbPrice();
if(promoCodeList.size()>0){
//有优惠码
BigDecimal onePrice = new BigDecimal("0.00");
int i;
for( i = 0;i= 0) {
return JsonMessage.error("商品价格异常");
}
String sn1 = orderService.initSn(equipmentId);
//获取分销 关联设备
TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
if (admin == null) {
return JsonMessage.error("找不到设备商家");
}
JSONArray altInfo = new JSONArray();
String orderNo1 = sn1;
BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
amount = amount.multiply(new BigDecimal(100));
String notifyUrl = JoinpayConstant.Notify_Url;
String frpCode1 = "WEIXIN_NATIVE";
TOrder order = new TOrder();
order.setId(IDGenerator.orderID());
order.setCreateDate(new Date());
order.setModifyDate(new Date());
order.setAdminId(admin.getId());
order.setSn(sn1);
order.setProductName(productName.toString());
order.setPrice(price);
order.setClientId(equipment.getClientId());
order.setEquipmentId(equipmentId);
order.setFrpCode(frpCode1);
order.setAltInfo(altInfo.toString());
order.setStatus(0);
String payPlatform = "3";
order.setPayPlatform(payPlatform);
if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
order.setIsSettlement("0");
}
order.setNote(note.toString());
orderService.save(order);
Iterator