package com.gree.mall.manager.logic.material.manage;

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.bean.material.manage.WebsitPartsConfigBean;
import com.gree.mall.manager.bean.material.manage.WebsitPartsConfigVO;
import com.gree.mall.manager.commonmapper.MaterialMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.MaterialConfig;
import com.gree.mall.manager.plus.entity.WebsitPartsRefundConfig;
import com.gree.mall.manager.plus.service.MaterialConfigService;
import com.gree.mall.manager.plus.service.WebsitPartsRefundConfigService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class WebsitPartsConfigLogic {

    private final CommonLogic commonLogic;
    private final MaterialMapper materialMapper;
    private final MaterialConfigService materialConfigService;
    private final WebsitPartsRefundConfigService websitPartsRefundConfigService;

    public IPage<WebsitPartsConfigVO> page(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.materialParam(zfireParamBean, WebsitPartsConfigVO.class, adminUser);
        IPage<WebsitPartsConfigVO> page = materialMapper.websitPartsConfigPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return page;
    }

    public WebsitPartsConfigBean detail(String id) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        final WebsitPartsRefundConfig config = websitPartsRefundConfigService.getById(id);

        WebsitPartsConfigBean bean = new WebsitPartsConfigBean();
        BeanUtils.copyProperties(config, bean);

        MaterialConfig centerConfig = materialConfigService.lambdaQuery()
                .eq(MaterialConfig::getCompanyWechatId, config.getCompanyWechatId())
                .last("limit 1")
                .one();
        bean.setPriceRate1(centerConfig.getPriceRate1());
        bean.setPriceRate2(centerConfig.getPriceRate2());
        bean.setPriceRate3(centerConfig.getPriceRate3());

        return bean;
    }

    public void add(WebsitPartsConfigBean bean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }
        if (Objects.isNull(bean.getNewRefundInner()) || bean.getNewRefundInner().compareTo(0) <= 0) {
            throw new RemoteServiceException("新件返还（市区）不能为空或少于等于0");
        }
        if (Objects.isNull(bean.getNewRefundOutside()) || bean.getNewRefundOutside().compareTo(0) <= 0) {
            throw new RemoteServiceException("新件返还（外区）不能为空或少于等于0");
        }
        if (Objects.isNull(bean.getOldRefund()) || bean.getOldRefund().compareTo(0) <= 0) {
            throw new RemoteServiceException("旧件返还不能为空或少于等于0");
        }
        final Integer defaultCount = websitPartsRefundConfigService.lambdaQuery()
                .eq(WebsitPartsRefundConfig::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .isNull(WebsitPartsRefundConfig::getWebsitId)
                .count();
        if (defaultCount <= 0) {
            throw new RemoteServiceException("缺少默认配置");
        }
        if (StringUtils.isNotBlank(bean.getWebsitId())) {
            final Integer existCount = websitPartsRefundConfigService.lambdaQuery()
                    .eq(WebsitPartsRefundConfig::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                    .eq(WebsitPartsRefundConfig::getWebsitId, bean.getWebsitId())
                    .count();
            if (existCount > 0) {
                throw new RemoteServiceException(bean.getWebsitName() + "已存在配置");
            }
        }
        WebsitPartsRefundConfig config = new WebsitPartsRefundConfig();
        config.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName())
                .setWebsitId(bean.getWebsitId())
                .setWebsitName(bean.getWebsitName())
                .setNewRefundInner(bean.getNewRefundInner())
                .setNewRefundOutside(bean.getNewRefundOutside())
                .setOldRefund(bean.getOldRefund())
                .insert();

        if (adminUser.getType() == 1) {
            MaterialConfig materialConfig = materialConfigService.lambdaQuery()
                    .eq(MaterialConfig::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                    .last("limit 1")
                    .one();
            materialConfig.setPriceRate1(bean.getPriceRate1())
                    .setPriceRate2(bean.getPriceRate2())
                    .setPriceRate3(bean.getPriceRate3())
                    .updateById();
        }
    }

    public void edit(WebsitPartsConfigBean bean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }
        WebsitPartsRefundConfig oldRefundConfig = websitPartsRefundConfigService.getById(bean.getId());
        if (oldRefundConfig.getIsDefault() && adminUser.getType() == 0) {
            throw new RemoteServiceException("非商户账号不能修改默认记录");
        }
        oldRefundConfig.setOldRefund(bean.getOldRefund())
                .setNewRefundInner(bean.getNewRefundInner())
                .setNewRefundOutside(bean.getNewRefundOutside())
                .updateById();

        if (adminUser.getType() == 1) {
            MaterialConfig materialConfig = materialConfigService.lambdaQuery()
                    .eq(MaterialConfig::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                    .last("limit 1")
                    .one();
            materialConfig.setPriceRate1(bean.getPriceRate1())
                    .setPriceRate2(bean.getPriceRate2())
                    .setPriceRate3(bean.getPriceRate3())
                    .updateById();
        }
    }

    public void batchDel(List<String> ids) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }
        final Integer count = websitPartsRefundConfigService.lambdaQuery()
                .in(WebsitPartsRefundConfig::getId, ids)
                .eq(WebsitPartsRefundConfig::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitPartsRefundConfig::getIsDefault, true)
                .count();
        if (count > 0) {
            throw new RemoteServiceException("默认记录不能删除");
        }
        websitPartsRefundConfigService.lambdaUpdate()
                .in(WebsitPartsRefundConfig::getId, ids)
                .eq(WebsitPartsRefundConfig::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitPartsRefundConfig::getIsDefault, false)
                .remove();
    }

    public WebsitPartsConfigBean getDefault() {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }
        WebsitPartsRefundConfig refundConfig = websitPartsRefundConfigService.lambdaQuery()
                .eq(WebsitPartsRefundConfig::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitPartsRefundConfig::getIsDefault, true)
                .one();
        MaterialConfig materialConfig = materialConfigService.lambdaQuery()
                .eq(MaterialConfig::getCompanyWechatId, adminUser.getCompanyWechatId())
                .one();

        WebsitPartsConfigBean bean = new WebsitPartsConfigBean();
        BeanUtils.copyProperties(refundConfig, bean);
        bean.setPriceRate1(materialConfig.getPriceRate1());
        bean.setPriceRate2(materialConfig.getPriceRate2());
        bean.setPriceRate3(materialConfig.getPriceRate3());

        return bean;
    }
}
