package com.zfire.mall.manager.logic.manage.product.price;

import com.zfire.mall.manager.bean.manage.product.price.PriceSaleTypeRelaBean;
import com.zfire.mall.manager.bean.wallet.WalletRebateBean;
import com.zfire.mall.manager.enums.StatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.wallet.WalletLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class PriceSaleTypeRelaLogic {

    @Resource
    PriceSaleTypeRelaService priceSaleTypeRelaService;
    @Resource
    PriceTypeService priceTypeService;
    @Resource
    SaleTypeService saleTypeService;
    @Autowired
    WalletRebateService walletRebateService;
    @Autowired
    KingDeeWalletService kingDeeWalletService;
    @Autowired
    WalletLogic walletLogic;

    public PriceSaleTypeRelaBean detail(String priceSaleTypeRelaId) {
        PriceSaleTypeRela rela = priceSaleTypeRelaService.lambdaQuery()
                .eq(PriceSaleTypeRela::getPriceSaleTypeRelaId, priceSaleTypeRelaId)
                .one();
        PriceSaleTypeRelaBean bean = new PriceSaleTypeRelaBean();
        BeanUtils.copyProperties(rela, bean);

        PriceType priceType = priceTypeService.lambdaQuery()
                .eq(PriceType::getPriceTypeId, bean.getPriceTypeId())
                .one();

        SaleType saleType = saleTypeService.lambdaQuery()
                .eq(SaleType::getId, bean.getSaleId())
                .one();

        bean.setSaleCode(saleType.getSaleCode());
        bean.setSaleName(saleType.getSaleName());
        bean.setPriceTypeCode(priceType.getPriceTypeCode());
        bean.setPriceTypeName(priceType.getPriceTypeName());

        return bean;
    }

    public void add(PriceSaleTypeRela priceSaleTypeRela) {
        if (StringUtils.isBlank(priceSaleTypeRela.getSaleId())) {
            throw new RemoteServiceException("销售类型不能为空");
        }
        if (StringUtils.isBlank(priceSaleTypeRela.getPriceTypeId())) {
            throw new RemoteServiceException("价格类型不能为空");
        }
        if(StringUtils.isBlank(priceSaleTypeRela.getWalletId())){
            throw new RemoteServiceException("钱包不能为空");
        }
        this.checkIsExistObj(priceSaleTypeRela);
        this.checkOnlyObj(priceSaleTypeRela);

        priceSaleTypeRela.insert();
    }

    /**
     * 检查是否唯一存在
     * @param priceSaleTypeRela
     */
    private void checkOnlyObj(PriceSaleTypeRela priceSaleTypeRela) {
        Integer count = priceSaleTypeRelaService.lambdaQuery()
                .ne(StringUtils.isNotBlank(priceSaleTypeRela.getPriceSaleTypeRelaId()), PriceSaleTypeRela::getPriceSaleTypeRelaId, priceSaleTypeRela.getPriceSaleTypeRelaId())
                .eq(PriceSaleTypeRela::getPriceTypeId, priceSaleTypeRela.getPriceTypeId())
                .eq(PriceSaleTypeRela::getSaleId, priceSaleTypeRela.getSaleId())
                .eq(PriceSaleTypeRela::getWalletId,priceSaleTypeRela.getWalletId())
                .count();
        if (count > 0) {
            throw new RemoteServiceException("价格与销售类型关系已存在");
        }
    }

    /**
     * 检查关系类型是否存在
     * @param priceSaleTypeRela
     */
    private void checkIsExistObj(PriceSaleTypeRela priceSaleTypeRela) {
        PriceType priceType = priceTypeService.lambdaQuery()
                .eq(PriceType::getPriceTypeId, priceSaleTypeRela.getPriceTypeId())
                .one();

        SaleType saleType = saleTypeService.lambdaQuery()
                .eq(SaleType::getId, priceSaleTypeRela.getSaleId())
                .one();

        KingDeeWallet wallet = walletLogic.kingDeedetail(priceSaleTypeRela.getWalletId());
        if(wallet == null){
            throw new RemoteServiceException("钱包不存在");
        }
        if(StringUtils.isNotBlank(priceSaleTypeRela.getWalletId2())) {
            WalletRebate walletRebate = walletRebateService.getById(priceSaleTypeRela.getWalletId2());
            if (walletRebate == null) {
                throw new RemoteServiceException("返利类型不存在");
            }
        }

        if (Objects.isNull(priceType)) {
            throw new RemoteServiceException("销售类型不存在");
        }
        if (Objects.isNull(saleType)) {
            throw new RemoteServiceException("价格类型不存在");
        }
    }

    public void edit(PriceSaleTypeRela priceSaleTypeRela) {
        if (StringUtils.isBlank(priceSaleTypeRela.getSaleId())) {
            throw new RemoteServiceException("销售类型不能为空");
        }
        if (StringUtils.isBlank(priceSaleTypeRela.getPriceTypeId())) {
            throw new RemoteServiceException("价格类型不能为空");
        }

        this.checkIsExistObj(priceSaleTypeRela);
        this.checkOnlyObj(priceSaleTypeRela);

        priceSaleTypeRela.updateById();
    }

    public void updateStatus(String priceSaleTypeRelaId, StatusEnum status) {
        priceSaleTypeRelaService.lambdaUpdate()
                .set(PriceSaleTypeRela::getStatus, status.getKey())
                .eq(PriceSaleTypeRela::getPriceSaleTypeRelaId, priceSaleTypeRelaId)
                .update();
    }

    public void del(List<String> ids) {
        priceSaleTypeRelaService.lambdaUpdate()
                .in(PriceSaleTypeRela::getPriceSaleTypeRelaId, ids)
                .remove();
    }


}
