WxCallBackController.java 6.37 KB
Newer Older
1 2 3 4 5 6
/**  
* <p>Title: WxCallBackController.java</p>  
* <p>Description: </p>  
* @author dsc  
* @date 2020年4月22日  
* @version 1.0  
7
*/
8 9
package cn.timer.api.callback.wx;

10 11 12 13 14 15
import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
16 17 18
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20 21
import org.springframework.web.bind.annotation.RestController;

22 23 24 25 26 27 28
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
29
import cn.timer.api.bean.qyzx.QyzxEntInfoM;
30 31 32
import cn.timer.api.bean.qyzx.businessService.QyzxBuyRecord;
import cn.timer.api.bean.qyzx.businessService.QyzxOrderRecord;
import cn.timer.api.bean.qyzx.businessService.QyzxRemainingQuantity;
33 34 35 36 37
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

38 39 40 41 42 43 44 45 46 47 48 49
/**
 * <p>
 * Title: WxCallBackController.java
 * </p>
 * <p>
 * Description: 微信回调接口类
 * </p>
 * 
 * @author dsc
 * @date 2020年4月22日
 * @version 1.0
 */
50 51

@RestController
52
@Transactional
53 54 55
@Api(tags = "99.98 微信回调接口")
@RequestMapping(value = "/callback/wx", produces = { "application/json" })
public class WxCallBackController {
56 57 58 59 60 61 62 63 64 65

	@Value("${spring.profiles.active}")
	private String env;

	@GetMapping(value = "/t")
	public String testEnv() {
		System.out.println(env);
		return env;
	}

66
	@PostMapping(value = "/pro")
67 68
	@ApiOperation(value = "购买服务(pro)", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> wxcallback_pro(@RequestBody String orderNo) { // 入参修改 TODO
69 70

		// 验证微信回调信息 TODO
71

72
		// 交易成功
73 74 75

//		updateAndInsert(orderNo);
		return ResultUtil.data(orderNo, "购买成功");
76 77

	}
78

79
	@PostMapping(value = "/test")
80 81 82 83 84
	@ApiOperation(value = "购买服务(test)", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> wxcallback_test(@RequestParam String orderNo) {
		if (!env.equals("dev") && !env.equals("test")) {
			return ResultUtil.error("请切换到开发/测试环境");
		}
85 86 87 88

		// TODO 加套餐 如果已经存在可用的套餐 ,怎么搞

		// TODO 如果是系统续费 update企业信息表
89 90 91
		updateAndInsert(orderNo);
		return ResultUtil.data(orderNo, "购买成功");
	}
92

93
	private void updateAndInsert(String orderNo) {
94
		// 修改订单表
95 96 97 98 99 100 101 102 103 104 105
		QyzxOrderRecord qyzxOrderRecord = QyzxOrderRecord.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxOrderRecord>().eq(QyzxOrderRecord::getOrderNo, orderNo)); // 查询该订单
		qyzxOrderRecord.setOrderStatus(1); // 修改订单状态
		qyzxOrderRecord.update(new LambdaUpdateWrapper<QyzxOrderRecord>().eq(QyzxOrderRecord::getOrderNo, orderNo)); // 更新数据

		// 插入套餐余量表
		String jsonStr = qyzxOrderRecord.getOrderDetail(); // json字符串
		JSONObject jsonObject = JSONUtil.parseObj(jsonStr); // json对象
		Integer expiration = jsonObject.getInt("expiration"); // 商品有效期(天)
		Date expireDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, expiration); // 时间偏移
		String content = jsonObject.getStr("content"); // 商品名称
106
		Integer specification = jsonObject.getInt("specification"); // 商品规格-用于系统续费表示为系统版本0-试用 1-普通 2-专业
107 108 109 110 111 112 113 114 115
		String unit = jsonObject.getStr("unit");
//				Double originalPrice = jsonObject.getDouble("originalPrice"); // 商品原价
		Double nowPrice = jsonObject.getDouble("nowPrice"); // 购买价
		Integer count = qyzxOrderRecord.getCount(); // 订单购买数量
		Integer pmid = qyzxOrderRecord.getPmid(); // 付费模块id
		Integer pcid = qyzxOrderRecord.getPcid(); // 付费内容id
		Integer createdUser = qyzxOrderRecord.getCreateUser(); // 订单创建人
		Integer orgCode = qyzxOrderRecord.getOrgCode(); // 公司
		Integer totalNum = specification * count; // 套餐总量
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		if (pmid == 3) {
			QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(orgCode).build().selectById(); // 企业信息
			Date endTime = qyzxEntInfoM.getEndTime(); // 当前到期时间
//			Integer level = qyzxEntInfoM.getLevel(); // 原系统版本 
//			Integer specification = jsonObject.getInt("specification"); // 新系统版本
			Date newEndTime = DateUtil.offsetDay(endTime, expiration*count); // 到期时间+商品有效期x购买数量=系统续费后的时间
			qyzxEntInfoM.update(new LambdaUpdateWrapper<QyzxEntInfoM>().eq(QyzxEntInfoM::getId, orgCode)
					.set(QyzxEntInfoM::getEndTime, newEndTime).set(QyzxEntInfoM::getLevel, specification));
		} else {
			QyzxRemainingQuantity quantity = new QyzxRemainingQuantity();
			quantity.setOrderNo(orderNo); // 订单号
			quantity.setContent(content); // 商品名称
			quantity.setExpireDate(expireDate); // 到期时间
			quantity.setSpecification(specification); // 套餐规格
			quantity.setTotalNum(totalNum); // 套餐总量
			quantity.setRemainder(totalNum); // 套餐剩余数(同上)
			quantity.setCount(count); // 购买数量
			quantity.setPmid(pmid); // 付费模块id
			quantity.setPcid(pcid); // 付费内容id
			quantity.setUnit(unit);
			quantity.setCreateUser(createdUser);
			quantity.setOrgCode(orgCode);
			quantity.insert();
		}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		// 插入购买记录表
		QyzxBuyRecord qyzxBuyRecord = new QyzxBuyRecord();
		qyzxBuyRecord.setSpecification(specification);
		qyzxBuyRecord.setUnit(unit);
		qyzxBuyRecord.setContent(content);
		qyzxBuyRecord.setCreateUser(createdUser);
		qyzxBuyRecord.setOrderNo(orderNo);
		qyzxBuyRecord.setPmid(pmid);
		qyzxBuyRecord.setPcid(pcid);
		qyzxBuyRecord.setPrice(nowPrice);
		qyzxBuyRecord.setCount(count);
		qyzxBuyRecord.setTotalPrice(nowPrice * count);
		qyzxBuyRecord.setExpireDate(expireDate);
		qyzxBuyRecord.setPayment(1); // 1-微信 2-支付宝
		qyzxBuyRecord.setInvoiceStatus(0); // 0-待开票 1-开票中 2-已开票 3-开票失败
		qyzxBuyRecord.setOrgCode(orgCode);
		qyzxBuyRecord.insert();
158 159 160
	}

}