package com.dk.oauth.service.integral; import com.dk.common.infrastructure.annotaiton.Pagination; import com.dk.common.mapper.BaseMapper; import com.dk.common.model.pojo.PageList; import com.dk.common.response.ResponseResultUtil; import com.dk.common.response.ResponseResultVO; import com.dk.common.service.BaseService; import com.dk.oauth.infrastructure.convert.integral.CouponSendConvert; import com.dk.oauth.mapper.integral.CouponMapper; import com.dk.oauth.mapper.integral.CouponReceiveMapper; import com.dk.oauth.mapper.integral.CouponSendMapper; import com.dk.oauth.model.VO.integral.CouponSendVO; import com.dk.oauth.model.pojo.integral.CouponSend; import com.dk.oauth.model.query.integral.CouponQuery; import com.dk.oauth.model.query.integral.CouponReceiveQuery; import com.dk.oauth.model.query.integral.CouponSendQuery; import com.dk.oauth.model.response.integral.CouponReceiveResponse; import com.dk.oauth.model.response.integral.CouponResponse; import com.dk.oauth.model.response.integral.CouponSendResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; @Service @Transactional public class CouponSendService extends BaseService { @Override public BaseMapper getRepository() { return couponSendMapper; } @Autowired private CouponSendMapper couponSendMapper; @Autowired //优惠券发出实体转换 private CouponSendConvert couponSendConvert; @Autowired //优惠券获取 private CouponReceiveMapper couponReceiveMapper; @Autowired //优惠券 private CouponMapper couponMapper; /** * @desc : 条件查询 * @date : 2024/7/25 10:22 * @author : 寇珊珊 */ @Pagination public ResponseResultVO> selectByCond(CouponSendQuery couponSendQuery) { return super.mergeListWithCount(couponSendQuery, couponSendMapper.selectByCond(couponSendQuery), couponSendMapper.countByCond(couponSendQuery)); } /** * @desc : 条件查询-历史查询 * @date : 2024/7/25 10:22 * @author : 寇珊珊 */ @Pagination public ResponseResultVO> selectByCondHistory(CouponSendQuery couponSendQuery) { return super.mergeListWithCount(couponSendQuery, couponSendMapper.selectByCondHistory(couponSendQuery), couponSendMapper.countByCond(couponSendQuery)); } /** * @desc : 新建 * @date : 2024/7/25 13:07 * @author : 寇珊珊 */ @Transactional(rollbackFor = {Exception.class}) public ResponseResultVO insert(CouponSendVO couponSendVO) { // 20240801 判断是否当前用户 有未过期的 发出id Map map = couponSendMapper.selectCouponSendItem(couponSendVO); if (map != null) { return ResponseResultUtil.success(map); } else { //region 新建 CouponSend couponSend = this.insertCouponSend(couponSendVO); //endregion // 返回请求数据时系统的当前时间 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); couponSend.setNowTime(now.format(formatter)); return ResponseResultUtil.success(couponSend); } // 20240801 // //region 根据id查询数据 // CouponSendResponse couponSendResponse = couponSendMapper.selectById(couponSendVO.getCouponId()); // if (couponSendResponse != null) { // //region 查询已助力人数 // List couponReceiveResponses = couponReceiveMapper.selectByCond(new CouponReceiveQuery().setCouponId(couponSendResponse.getCouponId())); // int userNum = couponReceiveResponses.size(); //// if(couponReceiveResponses.size()>couponSendResponse.getUserNum()){ //// //所需助力人数已满 //// throw new BaseBusinessException(ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getCode(),ErrorCodeEnum.THE_REQUIRED_NUMBER_OF_HELPERS_IS_FULL.getMessage()); //// } // //endregion // // //region 校验单据时间 // LocalDateTime endTime = couponSendMapper.selectEndTime(new CouponSendQuery() // .setCouponId(couponSendVO.getCouponId())) // .getEndDate(); // //原时间戳 // long primaryTime = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); // //本次时间戳 // long thisTime = couponSendVO.getEndDate().toInstant(ZoneOffset.of("+8")).toEpochMilli(); // //本次发出时间大于截止日期 // if ((thisTime / 1000) > (primaryTime / 1000)) { // // //region 新建 // this.insertCouponSend(couponSendVO); // //endregion // // } // //本次发出时间小于等于截止日期 并且助力人数未够 直接返回 // if ((thisTime / 1000) <= (primaryTime / 1000) && userNum < couponSendResponse.getUserNum()) { // return ResponseResultUtil.success(); // } // //本次发出时间小于等于截止日期 并且助力已满足 新建 // if ((thisTime / 1000) <= (primaryTime / 1000) && userNum == couponSendResponse.getUserNum()) { // // //region 新建 // this.insertCouponSend(couponSendVO); // //endregion // // 返回请求数据时系统的当前时间 // LocalDateTime now = LocalDateTime.now(); // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // couponSendVO.setNowTime(now.format(formatter)); // return ResponseResultUtil.success(couponSendVO); // } // //endregion // } // //endregion // // //region 不存在 新建 // else { // //region 新建 // this.insertCouponSend(couponSendVO); // //endregion // } // //endregion // // 返回请求数据时系统的当前时间 // LocalDateTime now = LocalDateTime.now(); // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // couponSendVO.setNowTime(now.format(formatter)); // return ResponseResultUtil.success(couponSendVO); } /** * @desc : 新建优惠券获取 * @date : 2024/7/26 10:58 * @author : 寇珊珊 */ @Transactional(rollbackFor = {Exception.class}) public CouponSend insertCouponSend(CouponSendVO couponSendVO) { //查询优惠券 List couponResponses = couponMapper.selectByCond(new CouponQuery()); //发出时间 couponSendVO.setSendDate(LocalDateTime.now()); //截止时间 Integer linkValidTimes = couponResponses.get(0).getLinkValidTimes(); couponSendVO.setEndDate(couponSendVO.getSendDate().plusHours(linkValidTimes)); CouponSend couponSend = couponSendConvert.convertToPo(couponSendVO); UUID uuid = UUID.randomUUID(); couponSend.setSendId(uuid.toString()); couponSendMapper.insert(couponSend); return couponSend; } /** * @desc : 获取助力明细 * @date : 2024/7/25 13:06 * @author : 刘尧 */ public ResponseResultVO getCouponSendItem(CouponSendVO couponSendVO) { return ResponseResultUtil.success(couponSendMapper.selectCouponSendItem(couponSendVO)); } }