package com.gree.mall.manager.logic.activity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.PromotionDiscount;
import com.gree.mall.manager.plus.service.PromotionDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PromotionDiscountLogic {

    @Autowired
    PromotionDiscountService promotionDiscountService;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 折扣活动列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<PromotionDiscount> list(Integer pageNum,Integer pageSize){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        IPage<PromotionDiscount> page = promotionDiscountService.lambdaQuery()
                .eq(PromotionDiscount::getCompanyId, adminUser.getCompanyId())
                .orderByDesc(PromotionDiscount::getCreateTime).page(new Page<>(pageNum, pageSize));
        return page;
    }

    /**
     * 新增折扣活动
     */
    public void add(PromotionDiscount promotionDiscount){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Integer count = promotionDiscountService.lambdaQuery()
                .eq(PromotionDiscount::getCompanyId, adminUser.getCompanyId())
                .eq(PromotionDiscount::getLimitUpperAmount,promotionDiscount.getLimitUpperAmount())
                .count();
        if(count > 0){
            throw new RemoteServiceException("折扣金额已存在，请勿重复设置");
        }
        if(promotionDiscount.getDiscountRate().doubleValue() <= 0 || promotionDiscount.getDiscountRate().doubleValue() >= 1){
            throw new RemoteServiceException("请填写大于0小于1的值");
        }

        promotionDiscount.setCompanyId(adminUser.getCompanyId());
        promotionDiscount.setCompanyName(adminUser.getCompanyName());
        promotionDiscount.setCreateTime(new Date());
        promotionDiscount.insert();
    }

    /**
     * 修改折扣活动
     */
    public void update(PromotionDiscount promotionDiscount){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Integer count = promotionDiscountService.lambdaQuery()
                .eq(PromotionDiscount::getCompanyId, adminUser.getCompanyId())
                .eq(PromotionDiscount::getLimitUpperAmount,promotionDiscount.getLimitUpperAmount())
                .ne(PromotionDiscount::getId,promotionDiscount.getId())
                .count();
        if(count > 0){
            throw new RemoteServiceException("折扣金额已存在，请勿重复设置");
        }
        if(promotionDiscount.getDiscountRate().doubleValue() <= 0 || promotionDiscount.getDiscountRate().doubleValue() >= 1){
            throw new RemoteServiceException("请填写大于0小于1的值");
        }
        promotionDiscount.updateById();
    }

    /**
     * 删除折扣活动
     */
    public void delete(String id){
        promotionDiscountService.removeById(id);
    }


    /**
     * 折扣活动-详情
     */
    public PromotionDiscount detail(String id){
        PromotionDiscount detail = promotionDiscountService.getById(id);
        return detail;
    }

}
