You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

323 lines
14 KiB

  1. package com.simple.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.simple.common.ErrorCode;
  6. import com.simple.common.Result;
  7. import com.simple.common.ResultData;
  8. import com.simple.domain.po.*;
  9. import com.simple.domain.vo.WxCouponCarVo;
  10. import com.simple.enums.EnumCarCmd;
  11. import com.simple.enums.EnumCarVendor;
  12. import com.simple.enums.EnumCouponStatus;
  13. import com.simple.service.*;
  14. import com.simple.utils.ETCPUtil;
  15. import com.simple.utils.TJDCarUtil;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiImplicitParams;
  18. import io.swagger.annotations.ApiOperation;
  19. import io.swagger.models.auth.In;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.apache.log4j.Logger;
  22. import org.apache.poi.hmef.attribute.MAPIAttribute;
  23. import org.omg.PortableInterceptor.INACTIVE;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. @RestController
  31. @RequestMapping("/car")
  32. public class WxCarController extends BaseController
  33. {
  34. private Logger logger = Logger.getLogger(WxCarController.class);
  35. ETCPUtil etcp = new ETCPUtil();
  36. TJDCarUtil tjd = new TJDCarUtil();
  37. @Autowired
  38. WxParkService wxParkService;
  39. @Autowired
  40. WxCUserCarService wxCUserCarService;
  41. @Autowired
  42. WxMerchantService wxMerchantService;
  43. @Autowired
  44. WxCarCmdLogService wxCarCmdLogService;
  45. @Autowired
  46. WxCouponService wxCouponService;
  47. @Autowired
  48. WxCouponCarService wxCouponCarService;
  49. private WxPark getCurrentPark(MallUserInfo user) {
  50. WxPark parkQ = new WxPark();
  51. parkQ.setTenantId(user.getTenantId());
  52. WxPark wxPark = wxParkService.getByObj(parkQ);
  53. // 1, get mall's park
  54. // future use redis to optimize
  55. return wxPark;
  56. }
  57. @ApiOperation(value = "获取车场支持的厂家",notes="{}")
  58. @GetMapping("/getVendor")
  59. public ResultData getVendor() {
  60. MallUserInfo user = getUser();
  61. // 1, get mall's park
  62. WxPark park = getCurrentPark(user);
  63. Map map = new HashMap();
  64. map.put("vendor", park.getVendorType());
  65. return new ResultData(map);
  66. }
  67. // 优免券模板
  68. /*
  69. {
  70. "id": 4974, //优免券模板 ID
  71. "parkId": "fbqXUlfVvfc=", //车场标识
  72. "businessId": "ZcWQ26TeJuQ=", //商家标识
  73. title -- "businessName": "天洋广场", //商家名称
  74. subtitle -- "name": "商家 1 小时优免券", //优免券名称
  75. "category": "1", //优免券类型 1:小时优惠券,2:金额优惠券,3:折扣优惠券,4:免费券
  76. price -- "categoryValue": "1.00", //优免券价值
  77. total -- "amount": 100000, //优免券数量
  78. valid_start_date -- "effectiveStart": "2018-02-01", //生效开始时间
  79. valid_end_date -- "effectiveEnd": "2028-02-01",//生效结束时间
  80. -"couponType": "0",//优惠券类型
  81. number 库存 -- "avaliavleNum": 99993 //可用优惠券
  82. },
  83. */
  84. @ApiOperation(value = "优免券模板", notes = "{\"merchantId\":\"商户ID\"}\n输出\n" +
  85. "{\n" +
  86. "\"data\":{" +
  87. "\"count\": 4," +
  88. "\"couponPlatformModels\": [\n" +
  89. " {\n" +
  90. " \"id\": 4974, //优免券模板 ID\n" +
  91. " \"parkId\": \"fbqXUlfVvfc=\", //车场标识" +
  92. " \"businessId\": \"ZcWQ26TeJuQ=\", //商家标识" +
  93. " \"businessName\": \"天洋广场\", //商家名称" +
  94. " \"name\": \"商家 1 小时优免券\", //优免券名称" +
  95. " \"category\": \"1\", //优免券类型\n 1:小时优惠券,2:金额优惠券,3:折扣优惠券,4:免费券" +
  96. " \"categoryValue\": \"1.00\", //优免券价值\n" +
  97. " \"amount\": 100000, //优免券数量\n" +
  98. " \"effectiveStart\": \"2018-02-01\", //生效开始时间\n" +
  99. " \"effectiveEnd\": \"2028-02-01\",//生效结束时间\n" +
  100. " \"couponType\": \"0\",//优惠券类型\n" +
  101. " \"avaliavleNum\": 99993 //可用优惠券\n" +
  102. " },\n" +
  103. " {...},\n" +
  104. " {...}\n" +
  105. " ]}}\n")
  106. @GetMapping("/quanTemplate")
  107. @ApiImplicitParams({
  108. @ApiImplicitParam(name="merchantId",value="商户ID",dataType="String", paramType = "query",required=true),
  109. @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
  110. @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
  111. public ResultData quanTemplate(String merchantId, Integer pageNum, Integer pageSize) {
  112. logger.info("quanTemplate: " + merchantId);
  113. MallUserInfo user = getUser();
  114. /// 1, get mall's park
  115. WxPark park = getCurrentPark(user);
  116. String params = park.getVendorParams();
  117. JSONObject objParams = JSON.parseObject(params);
  118. String url = objParams.getString("url");
  119. String merchantNo = objParams.getString("merchantNo");
  120. String merchantKey = objParams.getString("merchantKey");
  121. String version = objParams.getString("version");
  122. String parkId = park.getParkId();
  123. if (park.getVendorType() == EnumCarVendor.CAR_ETCP.getCode()) {
  124. if (StringUtils.isBlank(merchantId)) {
  125. // 优先从从商户表里取
  126. logger.error("quanTemplate failed, merchantId为空");
  127. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户为空");
  128. }
  129. // bussinessId from 参数信息
  130. String businessId = "";
  131. // 优先从从商户表里取
  132. businessId = getETCPBusinessID(merchantId);
  133. if (StringUtils.isBlank(businessId)) {
  134. // 1期只有一个虚拟商户,可以写在商场配置里
  135. businessId = objParams.getString("businessId");
  136. }
  137. String ret = etcp.getBCouponList(url, merchantNo, merchantKey, version, parkId, businessId);
  138. if (ret == null) {
  139. logger.error("quanTemplate failed, 优免券模板未发现");
  140. return new ResultData(ErrorCode.ETCP_CMD_FAIL.getCode(), "获取优免券模板异常");
  141. }
  142. JSONObject retObj = JSON.parseObject(ret);
  143. if (retObj.getIntValue("code") == 0) {
  144. return new ResultData(retObj.getJSONObject("data"));
  145. } else {
  146. logger.error("quanTemplate failed, 优免券模板未发现");
  147. return new ResultData(ErrorCode.ETCP_QUAN_TEMP_FAIL.getCode(), "优免券模板未发现", retObj);
  148. }
  149. }
  150. return new ResultData(ErrorCode.CAR_VENDOR_NOT_SUPPORT.getCode(), "优免券模板失败");
  151. }
  152. private String getETCPBusinessID(String merchantIdStr) {
  153. String businessId;
  154. Long merchantId = 0L;
  155. try {
  156. merchantId = Long.valueOf(merchantIdStr);
  157. } catch (NumberFormatException e) {
  158. logger.error(e.getMessage());
  159. }
  160. WxMerchant wxMerchant = wxMerchantService.getById(merchantId);
  161. String carParams = wxMerchant.getCarParams();
  162. JSONObject objParams1 = JSON.parseObject(carParams);
  163. businessId = objParams1.getString("businessId");
  164. return businessId;
  165. }
  166. @ApiOperation("新增停车券接口")
  167. @PostMapping("add")
  168. public ResultData add(@RequestBody WxCouponCarVo coupon) {
  169. logger.info(coupon.toString());
  170. //Assert.notNull(wxCoupon.getName(), "角色名不能为空");
  171. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  172. // Save to wx_counpon
  173. Date curDate = new Date();
  174. MallUserInfo user = getUser();
  175. // check 同一个模板的券分配额是否超了
  176. if (StringUtils.isBlank(coupon.getVendorParams())) {
  177. logger.error("请填充停车厂商优免券参数");
  178. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "停车厂商参数为空");
  179. }
  180. JSONObject vendorParamsObj = JSON.parseObject(coupon.getVendorParams());
  181. Long templateId = vendorParamsObj.getLong("id");
  182. Integer amount = vendorParamsObj.getInteger("amount");
  183. Integer avaliavleNum = vendorParamsObj.getInteger("avaliavleNum");
  184. Integer amtCount = wxCouponCarService.getAmtCountByTemplateId(templateId);
  185. Integer availCount = wxCouponCarService.getAvaibleCountByTemplateId(templateId);
  186. if (amtCount >= amount) {
  187. logger.error("已达到停车厂商优免券数量限制");
  188. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "已达到停车厂商优免券数量限制");
  189. }
  190. if (availCount >= avaliavleNum) {
  191. logger.error("已达到停车厂商优免券数量限制");
  192. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "已达到停车厂商优免券数量限制");
  193. }
  194. // check End
  195. WxCoupon wxCoupon = new WxCoupon();
  196. wxCoupon.setTenantId(user.getTenantId());
  197. wxCoupon.setMerchantId(coupon.getMerchantId());
  198. if(StringUtils.isNotEmpty(coupon.getSalePriceStr())){
  199. wxCoupon.setSalePrice((int)Double.parseDouble(coupon.getSalePriceStr())*100);
  200. }
  201. if(StringUtils.isNotEmpty(coupon.getUsePriceStr())){
  202. wxCoupon.setUsePrice((int)Double.parseDouble(coupon.getUsePriceStr())*100);
  203. }
  204. if(StringUtils.isNotEmpty(coupon.getPriceStr())){
  205. wxCoupon.setPrice((int)Double.parseDouble(coupon.getPriceStr())*100);
  206. }
  207. if(StringUtils.isNotBlank(coupon.getBusiness())) {
  208. String[] arys = coupon.getBusiness().split(",");
  209. wxCoupon.setBusiness(JSON.toJSONString(arys));
  210. }
  211. wxCoupon.setType(coupon.getType());
  212. wxCoupon.setCoverImg(coupon.getCoverImg());
  213. wxCoupon.setTitle(coupon.getTitle());
  214. wxCoupon.setSubTitle(coupon.getSubTitle());
  215. wxCoupon.setUseLimitQuantity(coupon.getUseLimitQuantity());
  216. wxCoupon.setTargetAd(coupon.getTargetAd());
  217. wxCoupon.setSendType(coupon.getSendType());
  218. wxCoupon.setValidType(coupon.getValidType());
  219. wxCoupon.setValidStartDate(coupon.getValidStartDate());
  220. wxCoupon.setValidEndDate(coupon.getValidEndDate());
  221. wxCoupon.setValidDays(coupon.getValidDays());
  222. wxCoupon.setDetail(coupon.getDetail());
  223. wxCoupon.setUnit(coupon.getUnit());
  224. wxCoupon.setRemainInventory(coupon.getRemainInventory());
  225. wxCoupon.setInventory(coupon.getInventory());
  226. wxCoupon.setRemark(coupon.getRemark());
  227. wxCoupon.setStatus(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode());
  228. wxCoupon.setCreateDate(curDate);
  229. wxCoupon.setUpdateDate(curDate);
  230. wxCoupon.setChannels("");
  231. Long id = wxCouponService.saveOrUpdate(wxCoupon);
  232. // Save to wx_coupon_car
  233. WxCouponCar couponCar = new WxCouponCar();
  234. couponCar.setId(id);
  235. couponCar.setTenantId(user.getTenantId());
  236. couponCar.setMerchantId(coupon.getMerchantId());
  237. WxPark park = getCurrentPark(user);
  238. couponCar.setParkId(park.getId());
  239. couponCar.setVendorType(park.getVendorType());
  240. couponCar.setVendorParams(coupon.getVendorParams());
  241. couponCar.setCreateDate(curDate);
  242. couponCar.setUpdateDate(curDate);
  243. wxCouponCarService.save(couponCar);
  244. return new ResultData(id);
  245. }
  246. @ApiOperation("优免券模板已分配总数")
  247. @GetMapping("/templateAmtCount")
  248. @ApiImplicitParams({
  249. @ApiImplicitParam(name="templateId",value="模板ID",dataType="Long", paramType = "query",required=true)})
  250. public ResultData getTemplateAmountSum(Long templateId) {
  251. Map map = new HashMap();
  252. Integer amountCount = 0;
  253. try {
  254. amountCount = wxCouponCarService.getAmtCountByTemplateId(templateId);
  255. } catch (Exception e) {
  256. logger.error(e.getMessage());
  257. }
  258. map.put("amountCount", amountCount);
  259. return new ResultData(map);
  260. }
  261. @ApiOperation("优免券模板库存总数")
  262. @GetMapping("/templateAvaiCount")
  263. @ApiImplicitParams({
  264. @ApiImplicitParam(name="templateId",value="模板ID",dataType="Long", paramType = "query",required=true)})
  265. public ResultData getTemplateAvailSum(Long templateId) {
  266. Map map = new HashMap();
  267. Integer availCount = 0;
  268. try {
  269. availCount = wxCouponCarService.getAvaibleCountByTemplateId(templateId);
  270. } catch (Exception e) {
  271. logger.error(e.getMessage());
  272. }
  273. map.put("availCount", availCount);
  274. return new ResultData(map);
  275. }
  276. @ApiOperation("停车券detail")
  277. @GetMapping("/detail")
  278. public ResultData getCouponCarDetail(@ModelAttribute WxCoupon coupon) {
  279. MallUserInfo user = getUser();
  280. coupon.setTenantId(user.getTenantId());
  281. try {
  282. WxCouponCarVo couponCarVo = wxCouponCarService.getByCoupon(coupon);
  283. if (couponCarVo != null) {
  284. return new ResultData(couponCarVo);
  285. } else {
  286. return new ResultData(ErrorCode.COUPON_IS_EMPTY.getCode(), "券不存在");
  287. }
  288. } catch (Exception e) {
  289. logger.error(e.getMessage());
  290. return new ResultData(ErrorCode.DB_FAIL.getCode(), e.getMessage());
  291. }
  292. }
  293. }