package com.crm.manager.controller;

import com.crm.manager.service.MarketPromoCodeFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.market.promo.code.MarketPromoCodeListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.market.promo.code.MarketPromoCodeAddEntity;
import com.crm.rely.backend.core.entity.market.promo.code.MarketPromoCodeSearchPageEntity;
import com.crm.rely.backend.core.entity.market.promo.code.MarketPromoCodeUpdateEntity;
import com.crm.rely.backend.core.entity.market.promo.code.MarketPromoCodeUpdateStatusEntity;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.UUIDUtil;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 营销群发邮件
 *
 * @author houn
 */
@RestController
@RequestMapping("/market/promo/code")
public class MarketPromoCodeController {

    @Autowired
    private MarketPromoCodeFeignService marketPromoCodeFeignService;

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated MarketPromoCodeAddEntity entity) throws Exception {

        //验证绑定邮箱
        if (entity.getBindEmail().equals(1) && Strings.isNullOrEmpty(entity.getEmail())) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        if (entity.getPromoType() == null) {
            entity.setPromoType(1);
        }
        if (entity.getPromoType().equals(1)) {
            if (entity.getAmountProportion() == null || entity.getAmountProportion() < 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
            if (entity.getAmountToplimit() == null) {
                entity.setAmountToplimit(BigDecimal.ZERO);
            }
            entity.setAmountFixed(null);
        }
        //如果固定金额小于0 系统错误
        if (entity.getPromoType().equals(2)) {
            if (BigDecimal.ZERO.compareTo(entity.getAmountFixed()) > 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
            entity.setAmountProportion(null);
            entity.setAmountToplimit(null);
        }

        return  marketPromoCodeFeignService.add(entity);
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated MarketPromoCodeUpdateEntity entity) throws Exception {

        //验证绑定邮箱
        if (entity.getBindEmail().equals(1) && Strings.isNullOrEmpty(entity.getEmail())) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        if (entity.getPromoType() == null) {
            entity.setPromoType(1);
        }
        if (entity.getPromoType().equals(1)) {
            if (entity.getAmountProportion() == null || entity.getAmountProportion() < 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
            if (entity.getAmountToplimit() == null) {
                entity.setAmountToplimit(BigDecimal.ZERO);
            }
            entity.setAmountFixed(null);
        }
        //如果固定金额小于0 系统错误
        if (entity.getPromoType().equals(2)) {
            if (BigDecimal.ZERO.compareTo(entity.getAmountFixed()) > 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
            entity.setAmountProportion(null);
            entity.setAmountToplimit(null);
        }
        return  marketPromoCodeFeignService.update(entity);
    }

    @PostMapping("/status")
    public BaseResultDto status(@RequestBody @Validated MarketPromoCodeUpdateStatusEntity entity) throws Exception {
        return  marketPromoCodeFeignService.status(entity);
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<MarketPromoCodeListDto> pageList(@RequestBody @Validated MarketPromoCodeSearchPageEntity entity) throws Exception {
        return  marketPromoCodeFeignService.pageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto<MarketPromoCodeListDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return  marketPromoCodeFeignService.single(entity);
    }
}
