package com.simple.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.simple.common.ErrorCode; import com.simple.common.Result; import com.simple.common.ResultData; import com.simple.domain.po.*; import com.simple.domain.vo.WxCouponCarVo; import com.simple.enums.EnumCarCmd; import com.simple.enums.EnumCarVendor; import com.simple.enums.EnumCouponStatus; import com.simple.service.*; import com.simple.utils.ETCPUtil; import com.simple.utils.TJDCarUtil; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.models.auth.In; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.apache.poi.hmef.attribute.MAPIAttribute; import org.omg.PortableInterceptor.INACTIVE; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/car") public class WxCarController extends BaseController { private Logger logger = Logger.getLogger(WxCarController.class); ETCPUtil etcp = new ETCPUtil(); TJDCarUtil tjd = new TJDCarUtil(); @Autowired WxParkService wxParkService; @Autowired WxCUserCarService wxCUserCarService; @Autowired WxMerchantService wxMerchantService; @Autowired WxCarCmdLogService wxCarCmdLogService; @Autowired WxCouponService wxCouponService; @Autowired WxCouponCarService wxCouponCarService; private WxPark getCurrentPark(MallUserInfo user) { WxPark parkQ = new WxPark(); parkQ.setTenantId(user.getTenantId()); WxPark wxPark = wxParkService.getByObj(parkQ); // 1, get mall's park // future use redis to optimize return wxPark; } @ApiOperation(value = "获取车场支持的厂家",notes="{}") @GetMapping("/getVendor") public ResultData getVendor() { MallUserInfo user = getUser(); // 1, get mall's park WxPark park = getCurrentPark(user); Map map = new HashMap(); map.put("vendor", park.getVendorType()); return new ResultData(map); } // 优免券模板 /* { "id": 4974, //优免券模板 ID "parkId": "fbqXUlfVvfc=", //车场标识 "businessId": "ZcWQ26TeJuQ=", //商家标识 title -- "businessName": "天洋广场", //商家名称 subtitle -- "name": "商家 1 小时优免券", //优免券名称 "category": "1", //优免券类型 1:小时优惠券,2:金额优惠券,3:折扣优惠券,4:免费券 price -- "categoryValue": "1.00", //优免券价值 total -- "amount": 100000, //优免券数量 valid_start_date -- "effectiveStart": "2018-02-01", //生效开始时间 valid_end_date -- "effectiveEnd": "2028-02-01",//生效结束时间 -"couponType": "0",//优惠券类型 number 库存 -- "avaliavleNum": 99993 //可用优惠券 }, */ @ApiOperation(value = "优免券模板", notes = "{\"merchantId\":\"商户ID\"}\n输出\n" + "{\n" + "\"data\":{" + "\"count\": 4," + "\"couponPlatformModels\": [\n" + " {\n" + " \"id\": 4974, //优免券模板 ID\n" + " \"parkId\": \"fbqXUlfVvfc=\", //车场标识" + " \"businessId\": \"ZcWQ26TeJuQ=\", //商家标识" + " \"businessName\": \"天洋广场\", //商家名称" + " \"name\": \"商家 1 小时优免券\", //优免券名称" + " \"category\": \"1\", //优免券类型\n 1:小时优惠券,2:金额优惠券,3:折扣优惠券,4:免费券" + " \"categoryValue\": \"1.00\", //优免券价值\n" + " \"amount\": 100000, //优免券数量\n" + " \"effectiveStart\": \"2018-02-01\", //生效开始时间\n" + " \"effectiveEnd\": \"2028-02-01\",//生效结束时间\n" + " \"couponType\": \"0\",//优惠券类型\n" + " \"avaliavleNum\": 99993 //可用优惠券\n" + " },\n" + " {...},\n" + " {...}\n" + " ]}}\n") @GetMapping("/quanTemplate") @ApiImplicitParams({ @ApiImplicitParam(name="merchantId",value="商户ID",dataType="String", paramType = "query",required=true), @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true), @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)}) public ResultData quanTemplate(String merchantId, Integer pageNum, Integer pageSize) { logger.info("quanTemplate: " + merchantId); MallUserInfo user = getUser(); /// 1, get mall's park WxPark park = getCurrentPark(user); String params = park.getVendorParams(); JSONObject objParams = JSON.parseObject(params); String url = objParams.getString("url"); String merchantNo = objParams.getString("merchantNo"); String merchantKey = objParams.getString("merchantKey"); String version = objParams.getString("version"); String parkId = park.getParkId(); if (park.getVendorType() == EnumCarVendor.CAR_ETCP.getCode()) { if (StringUtils.isBlank(merchantId)) { // 优先从从商户表里取 logger.error("quanTemplate failed, merchantId为空"); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户为空"); } // bussinessId from 参数信息 String businessId = ""; // 优先从从商户表里取 businessId = getETCPBusinessID(merchantId); if (StringUtils.isBlank(businessId)) { // 1期只有一个虚拟商户,可以写在商场配置里 businessId = objParams.getString("businessId"); } String ret = etcp.getBCouponList(url, merchantNo, merchantKey, version, parkId, businessId); if (ret == null) { logger.error("quanTemplate failed, 优免券模板未发现"); return new ResultData(ErrorCode.ETCP_CMD_FAIL.getCode(), "获取优免券模板异常"); } JSONObject retObj = JSON.parseObject(ret); if (retObj.getIntValue("code") == 0) { return new ResultData(retObj.getJSONObject("data")); } else { logger.error("quanTemplate failed, 优免券模板未发现"); return new ResultData(ErrorCode.ETCP_QUAN_TEMP_FAIL.getCode(), "优免券模板未发现", retObj); } } return new ResultData(ErrorCode.CAR_VENDOR_NOT_SUPPORT.getCode(), "优免券模板失败"); } private String getETCPBusinessID(String merchantIdStr) { String businessId; Long merchantId = 0L; try { merchantId = Long.valueOf(merchantIdStr); } catch (NumberFormatException e) { logger.error(e.getMessage()); } WxMerchant wxMerchant = wxMerchantService.getById(merchantId); String carParams = wxMerchant.getCarParams(); JSONObject objParams1 = JSON.parseObject(carParams); businessId = objParams1.getString("businessId"); return businessId; } @ApiOperation("新增停车券接口") @PostMapping("add") public ResultData add(@RequestBody WxCouponCarVo coupon) { logger.info(coupon.toString()); //Assert.notNull(wxCoupon.getName(), "角色名不能为空"); //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); // Save to wx_counpon Date curDate = new Date(); MallUserInfo user = getUser(); // check 同一个模板的券分配额是否超了 if (StringUtils.isBlank(coupon.getVendorParams())) { logger.error("请填充停车厂商优免券参数"); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "停车厂商参数为空"); } JSONObject vendorParamsObj = JSON.parseObject(coupon.getVendorParams()); Long templateId = vendorParamsObj.getLong("id"); Integer amount = vendorParamsObj.getInteger("amount"); Integer avaliavleNum = vendorParamsObj.getInteger("avaliavleNum"); Integer amtCount = wxCouponCarService.getAmtCountByTemplateId(templateId); Integer availCount = wxCouponCarService.getAvaibleCountByTemplateId(templateId); if (amtCount >= amount) { logger.error("已达到停车厂商优免券数量限制"); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "已达到停车厂商优免券数量限制"); } if (availCount >= avaliavleNum) { logger.error("已达到停车厂商优免券数量限制"); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "已达到停车厂商优免券数量限制"); } // check End WxCoupon wxCoupon = new WxCoupon(); wxCoupon.setTenantId(user.getTenantId()); wxCoupon.setMerchantId(coupon.getMerchantId()); if(StringUtils.isNotEmpty(coupon.getSalePriceStr())){ wxCoupon.setSalePrice((int)Double.parseDouble(coupon.getSalePriceStr())*100); } if(StringUtils.isNotEmpty(coupon.getUsePriceStr())){ wxCoupon.setUsePrice((int)Double.parseDouble(coupon.getUsePriceStr())*100); } if(StringUtils.isNotEmpty(coupon.getPriceStr())){ wxCoupon.setPrice((int)Double.parseDouble(coupon.getPriceStr())*100); } if(StringUtils.isNotBlank(coupon.getBusiness())) { String[] arys = coupon.getBusiness().split(","); wxCoupon.setBusiness(JSON.toJSONString(arys)); } wxCoupon.setType(coupon.getType()); wxCoupon.setCoverImg(coupon.getCoverImg()); wxCoupon.setTitle(coupon.getTitle()); wxCoupon.setSubTitle(coupon.getSubTitle()); wxCoupon.setUseLimitQuantity(coupon.getUseLimitQuantity()); wxCoupon.setTargetAd(coupon.getTargetAd()); wxCoupon.setSendType(coupon.getSendType()); wxCoupon.setValidType(coupon.getValidType()); wxCoupon.setValidStartDate(coupon.getValidStartDate()); wxCoupon.setValidEndDate(coupon.getValidEndDate()); wxCoupon.setValidDays(coupon.getValidDays()); wxCoupon.setDetail(coupon.getDetail()); wxCoupon.setUnit(coupon.getUnit()); wxCoupon.setRemainInventory(coupon.getRemainInventory()); wxCoupon.setInventory(coupon.getInventory()); wxCoupon.setRemark(coupon.getRemark()); wxCoupon.setStatus(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()); wxCoupon.setCreateDate(curDate); wxCoupon.setUpdateDate(curDate); wxCoupon.setChannels(""); Long id = wxCouponService.saveOrUpdate(wxCoupon); // Save to wx_coupon_car WxCouponCar couponCar = new WxCouponCar(); couponCar.setId(id); couponCar.setTenantId(user.getTenantId()); couponCar.setMerchantId(coupon.getMerchantId()); WxPark park = getCurrentPark(user); couponCar.setParkId(park.getId()); couponCar.setVendorType(park.getVendorType()); couponCar.setVendorParams(coupon.getVendorParams()); couponCar.setCreateDate(curDate); couponCar.setUpdateDate(curDate); wxCouponCarService.save(couponCar); return new ResultData(id); } @ApiOperation("优免券模板已分配总数") @GetMapping("/templateAmtCount") @ApiImplicitParams({ @ApiImplicitParam(name="templateId",value="模板ID",dataType="Long", paramType = "query",required=true)}) public ResultData getTemplateAmountSum(Long templateId) { Map map = new HashMap(); Integer amountCount = 0; try { amountCount = wxCouponCarService.getAmtCountByTemplateId(templateId); } catch (Exception e) { logger.error(e.getMessage()); } map.put("amountCount", amountCount); return new ResultData(map); } @ApiOperation("优免券模板库存总数") @GetMapping("/templateAvaiCount") @ApiImplicitParams({ @ApiImplicitParam(name="templateId",value="模板ID",dataType="Long", paramType = "query",required=true)}) public ResultData getTemplateAvailSum(Long templateId) { Map map = new HashMap(); Integer availCount = 0; try { availCount = wxCouponCarService.getAvaibleCountByTemplateId(templateId); } catch (Exception e) { logger.error(e.getMessage()); } map.put("availCount", availCount); return new ResultData(map); } @ApiOperation("停车券detail") @GetMapping("/detail") public ResultData getCouponCarDetail(@ModelAttribute WxCoupon coupon) { MallUserInfo user = getUser(); coupon.setTenantId(user.getTenantId()); try { WxCouponCarVo couponCarVo = wxCouponCarService.getByCoupon(coupon); if (couponCarVo != null) { return new ResultData(couponCarVo); } else { return new ResultData(ErrorCode.COUPON_IS_EMPTY.getCode(), "券不存在"); } } catch (Exception e) { logger.error(e.getMessage()); return new ResultData(ErrorCode.DB_FAIL.getCode(), e.getMessage()); } } }