package com.zteits.pay.biz; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import com.alibaba.fastjson.JSON; import com.clouds.common.utils.ListCopyUtil; import com.clouds.common.utils.SequenceUtil; import com.github.pagehelper.PageInfo; import com.zteits.clouds.api.apibase.bean.PageBean; import com.zteits.clouds.api.apibase.constants.CardTypeEnum; import com.zteits.clouds.api.apibase.constants.SequenceTypeEnum; import com.zteits.clouds.api.dto.pay.BindingPersonCouponRequest; import com.zteits.clouds.api.dto.pay.IsUsedByCardNoRequest; import com.zteits.clouds.api.dto.pay.param.*; import com.zteits.pay.pagepager.PageBeanUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSONObject; import com.clouds.common.cache.park.ParkingLotCacheUtil; import com.clouds.common.constants.YearAndShareCardNoConstant; import com.xiaoleilu.hutool.util.CollectionUtil; import com.zteits.clouds.api.apibase.bean.BizResult; import com.zteits.clouds.api.apibase.constants.DataStatusEnum; import com.zteits.clouds.api.apibase.constants.ErrorType; import com.zteits.clouds.api.apibase.exception.BizException; import com.zteits.clouds.api.dto.park.dto.ParkingLotDTO; import com.zteits.clouds.api.dto.park.param.QueryParkLotInfoByPkNoRequest; import com.zteits.clouds.api.dto.pay.HotAndColdCouponRuleDTO; import com.zteits.clouds.api.dto.pay.TdCCouponRuleDTO; import com.zteits.clouds.api.service.park.ParkingLotQueryService; import com.zteits.clouds.api.service.pay.TdCCouponRuleService; import com.zteits.pay.dao.TdCCouponPersonDao; import com.zteits.pay.dao.TdCCouponRuleDao; import com.zteits.pay.domain.TdCCouponPerson; import com.zteits.pay.domain.TdCCouponPersonDO; import com.zteits.pay.domain.TdCCouponRule; /** * Copyright: Copyright (c) 2017 zteits * * @Description: * @version: v1.0.0 * @author: langlw * @date: 2017/12/27 15 * Modification History: * Date Author Version Description * ---------------------------------------------------------* * 2017/12/27 langlw v1.0.0 创建 */ @Service("com.zteits.pay.biz.TdCCouponRuleServiceImpl") public class TdCCouponRuleServiceImpl implements TdCCouponRuleService { private static final Logger logger = LoggerFactory.getLogger(TdCCouponRuleServiceImpl.class); @Autowired private TdCCouponRuleDao tdCCouponRuleDao; @Autowired private TdCCouponPersonDao tdCCouponPersonDao; @Autowired private ParkingLotQueryService parkingLotQueryService; @Override public BizResult> queryCouponRuleList(TdCCouponRuleRequest request) { logger.info("查询优惠券规则表请求入参:request={}"+ JSONObject.toJSONString(request)); request.setQueryDate(new Date()); List list = tdCCouponRuleDao.queryCouponRuleList(request); logger.info("查询优惠券规则表响应结果:response={}"+ JSONObject.toJSONString(list)); return new BizResult<>(list); } @Override public BizResult> queryAllSellingCoupons(TdCCouponRuleRequest request) { logger.info("领券中心-获取所有上架卡劵请求入参:request={}"+ JSONObject.toJSONString(request)); //查询个人卡劵 if(StringUtils.isEmpty(request.getCustId())){ throw new BizException(ErrorType.PARAMM_NULL,"客户id为空!"); } TdCCouponPersonRequest personRequest = new TdCCouponPersonRequest(); BeanUtils.copyProperties(request, personRequest); personRequest.setQueryDate(new Date()); personRequest.setDataState(DataStatusEnum.DATA_STATUS_VALID.value()); List tdCCouponPersonDOs = tdCCouponPersonDao.getCouponPersonState(personRequest); List tdCCouponRuleDTOs = new ArrayList<>(); QueryParkLotInfoByPkNoRequest parkRequest = new QueryParkLotInfoByPkNoRequest(); BeanUtils.copyProperties(request, parkRequest); TdCCouponPersonRequest tdCCouponPersonRequest = new TdCCouponPersonRequest(); BeanUtils.copyProperties(request, tdCCouponPersonRequest); tdCCouponRuleDTOs = tdCCouponRuleDao.queryAllSellingCoupons(tdCCouponPersonRequest); //放入停车场名称和用户领取状态 for (TdCCouponRuleDTO tdCCouponRuleDTO : tdCCouponRuleDTOs) { tdCCouponRuleDTO.setReceiveState(0);//初始化为未领取 List plNames = new ArrayList<>(); if(StringUtils.isNotEmpty(tdCCouponRuleDTO.getPlNos())){ String []plNos = (tdCCouponRuleDTO.getPlNos()).split(","); for (String plNo : plNos) { parkRequest.setPklNo(plNo); BizResult parkingLot = parkingLotQueryService.QueryParkingLotByPkNo(parkRequest); plNames.add(parkingLot.getData().getPlName()); } } tdCCouponRuleDTO.setPlNames(plNames); //判断当前卡劵是否已领取 if(CollectionUtils.isNotEmpty(tdCCouponPersonDOs)){ for (TdCCouponPersonDO tdCCouponPersonDO : tdCCouponPersonDOs) { if((tdCCouponPersonDO.getCardNo().equals(tdCCouponRuleDTO.getCardNo()))){ tdCCouponRuleDTO.setReceiveState(1);//领取状态改为已领取 } } } } logger.info("领券中心-获取所有上架卡劵响应:request={}"+ JSONObject.toJSONString(tdCCouponRuleDTOs)); return new BizResult>(tdCCouponRuleDTOs); } @Override public BizResult> querySellingAllCoupons(TdCCouponPersonRequest request) { List tdCCouponRuleDTOs = new ArrayList<>(); request.setQueryDate(new Date()); tdCCouponRuleDTOs = tdCCouponRuleDao.queryAllSellingCoupons(request); return new BizResult<>(tdCCouponRuleDTOs); } @Transactional @Override public BizResult custReceiveCoupon(TdCCouponRuleRequest request) { TdCCouponRule couponRule = new TdCCouponRule(); logger.info("领券中心-客户根据卡劵编码领取相应的卡劵请求入参:request={}"+ JSONObject.toJSONString(request)); if(StringUtils.isEmpty(request.getCustId())){ throw new BizException(ErrorType.PARAMM_NULL,"客户id为空!"); } if(StringUtils.isEmpty(request.getCardNo())){ throw new BizException(ErrorType.PARAMM_NULL,"卡劵编码为空!"); } //先判断当前卡劵是否还有剩余 couponRule = tdCCouponRuleDao.queryCouponRuleByCardNo(request.getCardNo()); if(couponRule == null){ throw new BizException(ErrorType.NOTFOUND_COUPON,request.getCardNo()); } if(null == couponRule.getCardRestNum() || couponRule.getCardRestNum() < 1){ throw new BizException(ErrorType.COUPON_NOTREST,request.getCardNo()); } //修改卡劵表数量 couponRule.setCardRestNum(couponRule.getCardRestNum() -1); couponRule.setModfiyDate(new Date()); Boolean up = tdCCouponRuleDao.updateCouponRuleByCardNo(couponRule); if(!up){ throw new BizException(ErrorType.UPDATE_COUPON_FAIL,request.getCardNo()); } //新增个人卡劵表 TdCCouponPerson tdCCouponPerson = new TdCCouponPerson(); tdCCouponPerson.setCardNo(request.getCardNo()); tdCCouponPerson.setCreateDate(new Date()); tdCCouponPerson.setCustId(request.getCustId()); tdCCouponPerson.setCreateEmpid(request.getCustId()); tdCCouponPerson.setOrgId(couponRule.getOrgId()); tdCCouponPerson.setDataState(DataStatusEnum.DATA_STATUS_VALID.value()); Integer ins = tdCCouponPersonDao.insertCouponPerson(tdCCouponPerson); if(ins < 0){ throw new BizException(ErrorType.SAVE_COUPON_PERSON,ErrorType.SAVE_COUPON_PERSON.getMsg()); } return new BizResult(true); } @Override public BizResult queryHotAndColdCouponRule(QueryHotCouponRequest request) { List tdCCouponRuleDTOs = new ArrayList<>(); TdCCouponPersonRequest tdCCouponPersonRequest = new TdCCouponPersonRequest(); BeanUtils.copyProperties(request, tdCCouponPersonRequest); tdCCouponRuleDTOs = tdCCouponRuleDao.queryAllSellingCoupons(tdCCouponPersonRequest); /** 卡劵编码list*/ List cardNos = new ArrayList<>(); HotAndColdCouponRuleDTO hotAndColdCouponRuleDTO =new HotAndColdCouponRuleDTO(); if(CollectionUtils.isNotEmpty(tdCCouponRuleDTOs)){ for(TdCCouponRuleDTO dto:tdCCouponRuleDTOs){ cardNos.add(dto.getCardNo()); } request.setCardNos(cardNos); /**1、查询热门优惠券*/ List hotList = tdCCouponRuleDao.queryHotCouponRuleForList(request); /**2、查询冷门优惠券**/ List coldList = tdCCouponRuleDao.queryColdCouponRuleForList(request); hotAndColdCouponRuleDTO.setColdCouponList(coldList); hotAndColdCouponRuleDTO.setHotCouponList(hotList); } return new BizResult<>(hotAndColdCouponRuleDTO); } @Override public BizResult queryCouponRuleParkMsg(TdCCouponRuleRequest request) { logger.info("查询优惠券规则表请求入参:request={}"+ JSONObject.toJSONString(request)); List list = tdCCouponRuleDao.queryCouponRuleParkMsg(request); TdCCouponRuleDTO tdCCouponRuleDTO = new TdCCouponRuleDTO(); /**if(CollectionUtil.isNotEmpty(list)){ tdCCouponRuleDTO = list.get(0); List plNames = new ArrayList<>(); for (TdCCouponRuleDTO dto: list) { if(dto==null){ continue; } QueryParkLotInfoByPkNoRequest parkRequest=new QueryParkLotInfoByPkNoRequest(); parkRequest.setSysCode(request.getSysCode()); parkRequest.setPklNo(StringUtil.isEmpty(dto.getPlNo())?"":dto.getPlNo()); BizResult park = parkingLotQueryService.QueryParkingLotByPkNo(parkRequest); if(park==null || park.getData()==null){ continue; } plNames.add(park.getData().getPlName()); } tdCCouponRuleDTO.setPlNames(plNames); }*/ List plNos = new ArrayList<>(); if(CollectionUtil.isNotEmpty(list)){ tdCCouponRuleDTO = list.get(0); List plNames = new ArrayList<>(); for (TdCCouponRuleDTO dto: list) { if(dto==null || StringUtils.isEmpty(dto.getPlNo())){ continue; } plNos.add(dto.getPlNo()); } List parkList = ParkingLotCacheUtil.getParkLotByPlNos(plNos); if(CollectionUtils.isNotEmpty(parkList)){ for(ParkingLotDTO parkingLotDTO:parkList){ if(parkingLotDTO == null){ continue; } plNames.add(parkingLotDTO.getPlName()); } } tdCCouponRuleDTO.setPlNames(plNames); } return new BizResult(tdCCouponRuleDTO) ; } @Override public TdCCouponRuleDTO selectByCardNo(TdCCouponRuleRequest request) { return tdCCouponRuleDao.selectByCardNo(request); } /** * 保存优惠券卡规则.
* @param request * @return */ @Transactional @Override public BizResult saveCouponRule(TdCCouponRuleSaveReq request) { logger.info("begin保存优惠券卡规则信息,入参={}",JSONObject.toJSON(request)); Integer flg; if(Objects.equals(request.getCardType(), CardTypeEnum.CARD_TYPE_2.value())) { flg = this.saveCouponRuleList(request); }else{ flg = tdCCouponRuleDao.saveCouponRule(request); } logger.info("end保存优惠券卡规则信息"); return new BizResult<>(flg >0 ); } /** * 批量保存停车券 * @param * @return * @Author: xiejianpeng * @Date: 2019/3/21 15:20 * */ public Integer saveCouponRuleList(TdCCouponRuleSaveReq request){ List cardNos = new ArrayList<>(); List cardNosQuery = new ArrayList<>(); cardNos = this.getCardNoList(cardNos,cardNosQuery,request.getCardNum()); List tdCCouponRuleList = new ArrayList<>(); String batchNo = SequenceUtil.getNextId(SequenceTypeEnum.COUPON_RULE_PCH); /**对象中放入数据*/ for(String cardNo :cardNos) { TdCCouponRule tdCCouponRule = new TdCCouponRule(); BeanUtils.copyProperties(request,tdCCouponRule); tdCCouponRule.setBatchNo(batchNo); tdCCouponRule.setIsUesed(0); tdCCouponRule.setCardNo(cardNo); tdCCouponRule.setCardNum(1); tdCCouponRule.setReceiveNum(1); tdCCouponRule.setCardRestNum(1); tdCCouponRuleList.add(tdCCouponRule); } logger.info("begin保存优惠券卡规则信息,入参={}",JSONObject.toJSON(tdCCouponRuleList)); Integer flg = tdCCouponRuleDao.saveCouponRuleList(tdCCouponRuleList); logger.info("end保存优惠券卡规则信息,出参={}",JSONObject.toJSON(flg)); return flg; } /** * 生成停车卡的卡编码的数据 * @param * @return * @Author: xiejianpeng * @Date: 2019/3/21 14:39 * */ private List getCardNoList(List cardNos,List cardNosQuery,Integer cardNum){ logger.info("生成批量卡编码:{}",JSONObject.toJSON(cardNos)); if(cardNum > 0) { for (int i = 1; i <= cardNum; i++) { String cardNo = createBigStrOrNumberRadom(10); cardNos.add(cardNo); cardNosQuery.add(cardNo); } logger.info("查询是否存在重复的卡券编码入参:{}", JSONObject.toJSON(cardNosQuery)); if(CollectionUtil.isNotEmpty(cardNosQuery)) { cardNosQuery = tdCCouponRuleDao.queryCouponRuleByCardNoList(cardNosQuery); } logger.info("查询是否存在重复的卡券编码结果:{}", JSONObject.toJSON(cardNosQuery)); if(CollectionUtil.isNotEmpty(cardNosQuery)){ cardNos.removeAll(cardNosQuery); cardNum = cardNosQuery.size(); return getCardNoList(cardNos,cardNosQuery,cardNum); } } return cardNos; } /** * @param num * @return * @function 生成num位的随机字符串(小写字母与数字混排) */ public static String createBigStrOrNumberRadom(int num) { String str = ""; for (int i = 0; i < num; i++) { int intVal = (int) (Math.random() * 26 + 65); // if (intVal % 2 == 0) { // str += (char) intVal; // } else { str += (int) (Math.random() * 10); // } } return str; } /** * 更新优惠券卡规则.
* @param request * @return */ @Override public BizResult updateCouponRule(TdCCouponRuleSaveReq request) { logger.info("begin更新优惠券卡规则信息,入参={}",JSONObject.toJSON(request)); Integer integer = tdCCouponRuleDao.updateCouponRule(request); logger.info("end更新优惠券卡规则信息"); return new BizResult(integer >0 ); } /** * 删除优惠券卡规则.
* @param request * @return */ @Override public BizResult deleteCouponRule(TdCCouponRuleSaveReq request) { logger.info("begin删除优惠券卡规则信息,入参={}",JSONObject.toJSON(request)); Integer integer = tdCCouponRuleDao.deleteCouponRule(request); logger.info("end删除优惠券卡规则信息"); return new BizResult(integer >0 ); } /** * 分页查询优惠券规则 * @Auther: wangfs * @param * @return */ @Override public BizResult> queryCouponRuleForPage(QueryCouponRuleReq request) { logger.info("begin分页查询优惠券规则,入参={}",JSONObject.toJSON(request)); BizResult> result = new BizResult>(); PageBean pageBean = new PageBean(); PageInfo pageInfo = tdCCouponRuleDao.queryCouponRuleForPage(request); PageBeanUtil.copyProperties(pageInfo,pageBean,TdCCouponRuleDTO.class); logger.info("end分页查询优惠券规则"); return new BizResult>(pageBean); } /** * 过滤年终活动卡劵和好友分享卡劵 * @param tdCCouponRuleDTOs * @return * 2018年1月26日 wangfei */ private List filterYearOrShareCard(List tdCCouponRuleDTOs){ List result = new ArrayList<>(); List removeList = new ArrayList<>(); if(CollectionUtils.isNotEmpty(tdCCouponRuleDTOs)){ for (TdCCouponRuleDTO dto : tdCCouponRuleDTOs) { if(YearAndShareCardNoConstant.yearCardNos.contains(dto.getCardNo()) || YearAndShareCardNoConstant.shareCardNos.contains(dto.getCardNo())){ removeList.add(dto); } } } tdCCouponRuleDTOs.removeAll(removeList); result = tdCCouponRuleDTOs; return result; } @Override @Transactional public BizResult bindingPersonCoupon(BindingPersonCouponRequest request) { logger.info("绑定个人优惠券入参:"+ JSON.toJSONString(request)); //判断该优惠券是否已经被使用 final TdCCouponRule usedByCardNo = _isUsedByCardNo(request.getCardNo()); //修改为已绑定 TdCCouponRuleSaveReq updateCouponRule = new TdCCouponRuleSaveReq(); updateCouponRule.setIsUesed(1); updateCouponRule.setId(usedByCardNo.getId()); updateCouponRule.setModfiyDate(new Date()); tdCCouponRuleDao.updateCouponRule(updateCouponRule); //新增个人卡劵表 TdCCouponPerson tdCCouponPerson = new TdCCouponPerson(); tdCCouponPerson.setCardNo(request.getCardNo()); tdCCouponPerson.setCreateDate(new Date()); tdCCouponPerson.setCustId(request.getCustId()); tdCCouponPerson.setCreateEmpid(request.getCustId()); tdCCouponPerson.setOrgId(usedByCardNo.getOrgId()); tdCCouponPerson.setBeginTime(usedByCardNo.getBeginTime()); tdCCouponPerson.setEndTime(usedByCardNo.getEndTime()); tdCCouponPerson.setDataState(DataStatusEnum.DATA_STATUS_VALID.value()); tdCCouponPersonDao.insertCouponPerson(tdCCouponPerson); return new BizResult<>(true); } /** * 判断该优惠券是否已经被使用 * @param request */ @Override public BizResult isUsedByCardNo(IsUsedByCardNoRequest request) { final TdCCouponRule tdCCouponRule = _isUsedByCardNo(request.getCardNo()); TdCCouponRuleDTO tdCCouponRuleDTO = new TdCCouponRuleDTO(); BeanUtils.copyProperties(tdCCouponRule,tdCCouponRuleDTO); return new BizResult<>(tdCCouponRuleDTO); } @Override public BizResult updateCouponRuleByCardNo(TdCCouponRuleSaveReq req) { TdCCouponRule couponrule = new TdCCouponRule(); BeanUtils.copyProperties(req,couponrule); couponrule.setCardNo(req.getCardNo()); tdCCouponRuleDao.updateCouponRuleByCardNo(couponrule); return new BizResult<>(true); } @Override public BizResult deleteBatchCouponRule(TdCCouponRuleDeleteReq request) { logger.info("begin批量删除优惠券卡规则信息,入参={}",JSONObject.toJSON(request)); Integer integer = tdCCouponRuleDao.deleteBatchCouponRule(request); logger.info("end批量删除优惠券卡规则信息"); return new BizResult(integer >0 ); } private TdCCouponRule _isUsedByCardNo(String cardNo) { //查询该优惠券是否已经被使用 final TdCCouponRule tdCCouponRule = tdCCouponRuleDao.queryCouponRuleByCardNo(cardNo); if(tdCCouponRule == null){ throw new BizException(ErrorType.PARAM_NOT_VALID,"该优惠码无效",false,false); } //判断该优惠码是否是停车券(线下兑换使用),其他码不允许绑定 if(tdCCouponRule.getCardType() != CardTypeEnum.CARD_TYPE_2.value()){ throw new BizException(ErrorType.PARAM_NOT_VALID,"该优惠码不正确",false,false); } //获取是否已使用 1:已使用/绑定 0:未使用 if(tdCCouponRule.getIsUesed()!=null && tdCCouponRule.getIsUesed()!=0){ throw new BizException(ErrorType.PARAM_NOT_VALID,"该优惠码已经被使用或绑定",false,false); } return tdCCouponRule; } /** * 根据优惠券规则CardNo修改优惠券规则为无效 * * @param request * @return */ @Override public BizResult updateCouponRuleNoValidByCardNo(TdCCouponRuleSaveReq request) { logger.info("begin更新优惠券卡规则信息无效,入参={}", JSONObject.toJSON(request)); Integer integer = tdCCouponRuleDao.updateCouponRuleNoValidByCardNo(request); logger.info("end更新优惠券卡规则信息无效"); return new BizResult<>(integer > 0); } /** * 查询所有有效的优惠规则 * @param request * @return */ @Override public BizResult> getCouponRuleList(TdCCouponRuleRequest request) { List sourceList = tdCCouponRuleDao.getCouponRuleList(request); List targetList = new ArrayList<>(); ListCopyUtil.listCopyProperties(sourceList, targetList, TdCCouponRuleDTO.class); return new BizResult<>(targetList); } }