|  |  |  | 
|---|
|  |  |  | import com.mzl.flower.base.R; | 
|---|
|  |  |  | import com.mzl.flower.base.ReturnDataDTO; | 
|---|
|  |  |  | import com.mzl.flower.config.exception.ValidationException; | 
|---|
|  |  |  | import com.mzl.flower.dto.request.coupon.CreateCouponTemplateActivyDTO; | 
|---|
|  |  |  | import com.mzl.flower.dto.request.coupon.CreateCouponTemplateBO; | 
|---|
|  |  |  | import com.mzl.flower.dto.request.coupon.CreateCouponTemplateUserDTO; | 
|---|
|  |  |  | import com.mzl.flower.dto.request.coupon.QueryCouponDTO; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.mzl.flower.dto.response.coupon.CouponTemplateUserVO; | 
|---|
|  |  |  | import com.mzl.flower.dto.response.coupon.CouponTemplateVO; | 
|---|
|  |  |  | import com.mzl.flower.entity.coupon.CouponTemplateDO; | 
|---|
|  |  |  | import com.mzl.flower.enums.CouponCategoryEnum; | 
|---|
|  |  |  | import com.mzl.flower.enums.CouponGetUserTypeEnum; | 
|---|
|  |  |  | import com.mzl.flower.enums.CouponTypeEnum; | 
|---|
|  |  |  | import com.mzl.flower.enums.CouponUsageTypeEnum; | 
|---|
|  |  |  | import com.mzl.flower.enums.*; | 
|---|
|  |  |  | import com.mzl.flower.service.coupon.CouponTemplateService2; | 
|---|
|  |  |  | import com.mzl.flower.utils.ConverterUtil; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | import io.swagger.annotations.ApiOperation; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.http.ResponseEntity; | 
|---|
|  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "新增", notes = "新增") | 
|---|
|  |  |  | public ResponseEntity<ReturnDataDTO> create(@Validated @RequestBody CreateCouponTemplateUserDTO dto) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dto.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType()) && dto.getMinOrderAmount().compareTo(dto.getCouponDiscountValue()) < 0) { | 
|---|
|  |  |  | throw new ValidationException("订单金额不能小于折扣金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dto.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType()) && dto.getMinOrderAmount().compareTo(BigDecimal.ZERO) != 0) { | 
|---|
|  |  |  | throw new ValidationException("无门槛的订单金额必须为0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | valid(dto); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CreateCouponTemplateBO couponTemplateBO = new CreateCouponTemplateBO(); | 
|---|
|  |  |  | BeanUtils.copyProperties(dto, couponTemplateBO); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 设置成指定用户 | 
|---|
|  |  |  | couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.POINT.getType()); | 
|---|
|  |  |  | // 设置成积分优惠券 | 
|---|
|  |  |  | couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.TARGET.getType()); | 
|---|
|  |  |  | // 设置成用户优惠券 | 
|---|
|  |  |  | couponTemplateBO.setCategory(CouponCategoryEnum.USER.getStatus()); | 
|---|
|  |  |  | // 设置默认类型 | 
|---|
|  |  |  | couponTemplateBO.setUsageType(CouponUsageTypeEnum.GET_AFTER_TIME.getType()); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dto.setId(id); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dto.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType()) && dto.getMinOrderAmount().compareTo(dto.getCouponDiscountValue()) < 0) { | 
|---|
|  |  |  | throw new ValidationException("订单金额不能小于折扣金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dto.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType()) && dto.getMinOrderAmount().compareTo(BigDecimal.ZERO) != 0) { | 
|---|
|  |  |  | throw new ValidationException("无门槛的订单金额必须为0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | valid(dto); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getId()); | 
|---|
|  |  |  | if (null == couponTemplateDO) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CreateCouponTemplateBO couponTemplateBO = new CreateCouponTemplateBO(); | 
|---|
|  |  |  | BeanUtils.copyProperties(couponTemplateDO,couponTemplateBO); | 
|---|
|  |  |  | BeanUtils.copyProperties(dto, couponTemplateBO); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 设置成指定用户 | 
|---|
|  |  |  | couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.POINT.getType()); | 
|---|
|  |  |  | // 设置成积分优惠券 | 
|---|
|  |  |  | couponTemplateBO.setGetUserType(CouponGetUserTypeEnum.TARGET.getType()); | 
|---|
|  |  |  | // 设置成用户优惠券 | 
|---|
|  |  |  | couponTemplateBO.setCategory(CouponCategoryEnum.USER.getStatus()); | 
|---|
|  |  |  | // 设置默认类型 | 
|---|
|  |  |  | couponTemplateBO.setUsageType(CouponUsageTypeEnum.GET_AFTER_TIME.getType()); | 
|---|
|  |  |  | 
|---|
|  |  |  | public ResponseEntity<ReturnDataDTO<Page<CouponTemplatePointVO>>> page( | 
|---|
|  |  |  | Page page, QueryCouponDTO dto | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | // 设置只查询积分优惠券的 | 
|---|
|  |  |  | // 设置只查询用户优惠券的 | 
|---|
|  |  |  | dto.setCategory(CouponCategoryEnum.USER.getStatus()); | 
|---|
|  |  |  | Page<CouponTemplateVO> resultPage = couponTemplateService.getPage(page, dto); | 
|---|
|  |  |  | return returnData(R.SUCCESS.getCode(), ConverterUtil.transPage(resultPage, CouponTemplateUserVO.class)); | 
|---|
|  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "查询-全部", notes = "查询-全部") | 
|---|
|  |  |  | public ResponseEntity<ReturnDataDTO<Page<CouponTemplateVO>>> list(QueryCouponDTO dto | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | // 设置只查询积分优惠券的 | 
|---|
|  |  |  | // 设置只查询用户优惠券的 | 
|---|
|  |  |  | dto.setCategory(CouponCategoryEnum.USER.getStatus()); | 
|---|
|  |  |  | return returnData(R.SUCCESS.getCode(), ConverterUtil.transList(couponTemplateService.getList(dto), CouponTemplateUserVO.class)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | CouponTemplateDO couponTemplateDO = couponTemplateService.getById(id); | 
|---|
|  |  |  | if (null == couponTemplateDO) { | 
|---|
|  |  |  | throw new ValidationException("优惠券不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(StringUtils.isNotBlank(couponTemplateDO.getStatus()) | 
|---|
|  |  |  | &&  couponTemplateDO.getStatus().equals(CouponStatusEnum.ACTIVE.getStatus())){ | 
|---|
|  |  |  | throw new ValidationException("已发布的的商品不可重复发布"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(StringUtils.isNotBlank(couponTemplateDO.getStatus()) | 
|---|
|  |  |  | &&  couponTemplateDO.getStatus().equals(CouponStatusEnum.EXPIRED.getStatus())){ | 
|---|
|  |  |  | throw new ValidationException("已下架的商品不可重复上架"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | couponTemplateService.activeCouponTemplate(id); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void valid(CreateCouponTemplateUserDTO dto){ | 
|---|
|  |  |  | if (StringUtils.isNotBlank(dto.getCouponDiscountType()) && dto.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType())) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(dto.getMinOrderAmount().compareTo(BigDecimal.ZERO)<=0){ | 
|---|
|  |  |  | throw new ValidationException("优惠券使用条件不能小于0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(dto.getMinOrderAmount().compareTo(dto.getCouponDiscountValue()) < 0){ | 
|---|
|  |  |  | throw new ValidationException("优惠券使用条件不能小于面值金额"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dto.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType()) && dto.getMinOrderAmount().compareTo(BigDecimal.ZERO) != 0) { | 
|---|
|  |  |  | throw new ValidationException("无门槛的订单金额必须为0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|