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

import cn.hutool.core.util.StrUtil;
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.workorder.InstallSettleNormVO;
import com.gree.mall.manager.bean.workorder.OtherSettleNormVO;
import com.gree.mall.manager.bean.workorder.RepairSettleNormVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.enums.workorder.SettleNormTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
import com.gree.mall.manager.plus.entity.PgSettleNorm;
import com.gree.mall.manager.plus.service.PgSettleNormService;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
@RequiredArgsConstructor
public class SettleNormLogic {
    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final PgSettleNormService pgSettleNormService;

    public IPage<InstallSettleNormVO> installList(ZfireParamBean zfireParamBean) {
        FieldUtils.supplyParam(zfireParamBean, InstallSettleNormVO.class, commonLogic.getAdminUser());
        IPage<InstallSettleNormVO> page = commonMapper.installNormList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<RepairSettleNormVO> repairList(ZfireParamBean zfireParamBean) {
        FieldUtils.supplyParam(zfireParamBean, RepairSettleNormVO.class, commonLogic.getAdminUser());
        IPage<RepairSettleNormVO> page = commonMapper.repairNormList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<OtherSettleNormVO> otherList(ZfireParamBean zfireParamBean) {
        FieldUtils.supplyParam(zfireParamBean, OtherSettleNormVO.class, commonLogic.getAdminUser());
        IPage<OtherSettleNormVO> page = commonMapper.otherNormList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public PgSettleNorm detail(String id) {
        return pgSettleNormService.getById(id);
    }

    @Transactional
    public void add(PgSettleNorm norm) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (Objects.nonNull(adminUser.getAdminCompanyWechat())) {
            norm.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
            norm.setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName());
        }
        norm.setType(StrUtil.trim(norm.getType()));
        norm.setTypeName(StrUtil.trim(norm.getTypeName()));
        norm.setLabel(StrUtil.trim(norm.getLabel()));

        this.checkParams(norm, false);

        norm.insert();
    }

    private void checkParams(PgSettleNorm norm, boolean isEdit) {
        if (StringUtils.isBlank(norm.getOrderChannelId()) || StringUtils.isBlank(norm.getOrderChannelText())) {
            throw new RemoteServiceException("工单来源不能为空");
        }
        if (norm.getSettleNormType().equals(SettleNormTypeEnum.INSTALL.getKey())
            || norm.getSettleNormType().equals(SettleNormTypeEnum.REPAIR.getKey())) {
            if (StringUtils.isBlank(norm.getType()) || StringUtils.isBlank(norm.getTypeName())) {
                throw new RemoteServiceException("工单类型不能为空");
            }
            if (StringUtils.isBlank(norm.getParentCategoryId()) || StringUtils.isBlank(norm.getParentCategoryName())) {
                throw new RemoteServiceException("产品大类不能为空");
            }
            if (StringUtils.isBlank(norm.getCategoryId()) || StringUtils.isBlank(norm.getCategoryName())) {
                throw new RemoteServiceException("产品小类不能为空");
            }
        }

        if (norm.getSettleNormType().equals(SettleNormTypeEnum.INSTALL.getKey())) {
            if (StringUtils.isBlank(norm.getLabel())) {
                throw new RemoteServiceException("功率不能为空");
            }
            if (Objects.isNull(norm.getNormAmount())) {
                throw new RemoteServiceException("结算标准不能为空");
            }
            // 检查记录唯一 工单类型 产品大类 产品小类 功率
            Integer count = pgSettleNormService.lambdaQuery()
                    .eq(PgSettleNorm::getCompanyWechatId, norm.getCompanyWechatId())
                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.INSTALL.getKey())
                    .eq(PgSettleNorm::getOrderChannelId, norm.getOrderChannelId())
                    .eq(PgSettleNorm::getType, norm.getType())
                    .eq(PgSettleNorm::getParentCategoryId, norm.getParentCategoryId())
                    .eq(PgSettleNorm::getCategoryId, norm.getCategoryId())
                    .eq(PgSettleNorm::getLabel, norm.getLabel())
                    .ne(isEdit, PgSettleNorm::getId, norm.getId())
                    .count();

            if (count > 0) {
                throw new RemoteServiceException("已有\"工单类型 产品大类 产品小类 功率\"记录, 操作失败");
            }
        }

        if (norm.getSettleNormType().equals(SettleNormTypeEnum.REPAIR.getKey())) {
            if (StringUtils.isBlank(norm.getLabel())) {
                throw new RemoteServiceException("质保类型不能为空");
            }
            if (Objects.isNull(norm.getRepairAmount())) {
                throw new RemoteServiceException("上门费不能为空");
            }
            if (Objects.isNull(norm.getNormAmount())) {
                throw new RemoteServiceException("维修费结算比例不能为空");
            }
            // 检查记录唯一 工单类型 产品大类 产品小类 质保
            Integer count = pgSettleNormService.lambdaQuery()
                    .eq(PgSettleNorm::getCompanyWechatId, norm.getCompanyWechatId())
                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.REPAIR.getKey())
                    .eq(PgSettleNorm::getOrderChannelId, norm.getOrderChannelId())
                    .eq(PgSettleNorm::getType, norm.getType())
                    .eq(PgSettleNorm::getParentCategoryId, norm.getParentCategoryId())
                    .eq(PgSettleNorm::getCategoryId, norm.getCategoryId())
                    .eq(PgSettleNorm::getLabel, norm.getLabel())
                    .ne(isEdit, PgSettleNorm::getId, norm.getId())
                    .count();

            if (count > 0) {
                throw new RemoteServiceException("已有\"工单类型 产品大类 产品小类 质保类型\"记录, 操作失败");
            }
        }

        if (norm.getSettleNormType().equals(SettleNormTypeEnum.OTHER.getKey())) {
            if (StringUtils.isBlank(norm.getType())) {
                throw new RemoteServiceException("其他类型不能为空");
            }
            norm.setTypeName(norm.getType());
            if (Objects.isNull(norm.getLabel())) {
                throw new RemoteServiceException("规格不能为空");
            }
            if (Objects.isNull(norm.getNormAmount())) {
                throw new RemoteServiceException("单价不能为空");
            }
            // 检查记录唯一 其它费用类型 规格
            Integer count = pgSettleNormService.lambdaQuery()
                    .eq(PgSettleNorm::getCompanyWechatId, norm.getCompanyWechatId())
                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.OTHER.getKey())
                    .eq(PgSettleNorm::getOrderChannelId, norm.getOrderChannelId())
                    .eq(PgSettleNorm::getType, norm.getType())
                    .eq(PgSettleNorm::getLabel, norm.getLabel())
                    .ne(isEdit, PgSettleNorm::getId, norm.getId())
                    .count();

            if (count > 0) {
                throw new RemoteServiceException("已有\"其它费用类型 规格\"记录, 操作失败");
            }
        }
    }

    @Transactional
    public void edit(PgSettleNorm norm) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        PgSettleNorm old = pgSettleNormService.getById(norm.getId());
        norm.setCompanyWechatId(old.getCompanyWechatId())
                .setCompanyWechatName(old.getCompanyWechatName());
        norm.setType(StrUtil.trim(norm.getType()));
        norm.setTypeName(StrUtil.trim(norm.getTypeName()));
        norm.setLabel(StrUtil.trim(norm.getLabel()));

        this.checkParams(norm, true);
        norm.updateById();
    }

    public void delete(String id) {
        pgSettleNormService.lambdaUpdate()
                .eq(PgSettleNorm::getId, id)
                .remove();
    }

    public List<PgSettleNorm> installQuery(String settleNormType, String type, String mainId, String smallId, String label) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (StringUtils.isBlank(settleNormType) && StringUtils.isBlank(type)) {
            return new ArrayList<>();
        }

        AdminCompanyWechat adminCompanyWechat = adminUser.getAdminCompanyWechat();


        List<PgSettleNorm> list = pgSettleNormService.lambdaQuery()
                    .eq(Objects.nonNull(adminCompanyWechat), PgSettleNorm::getCompanyWechatId, Objects.nonNull(adminCompanyWechat) ? adminCompanyWechat.getCompanyWechatId() : "")
                    .eq(PgSettleNorm::getSettleNormType, settleNormType)
                    .eq(PgSettleNorm::getType, type)
                    .list();
//        List<PgSettleNorm> list = null;
//
//        if (StringUtils.isBlank(mainId) && StringUtils.isBlank(smallId) && StringUtils.isBlank(label)) {
//            list = pgSettleNormService.lambdaQuery()
//                    .eq(PgSettleNorm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(PgSettleNorm::getSettleNormType, settleNormType)
//                    .eq(PgSettleNorm::getType, type)
//                    .groupBy(PgSettleNorm::getParentCategoryId)
//                    .list();
//        } else if (StringUtils.isNotBlank(mainId) && StringUtils.isBlank(smallId) && StringUtils.isBlank(label)) {
//            list = pgSettleNormService.lambdaQuery()
//                    .eq(PgSettleNorm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(PgSettleNorm::getSettleNormType, settleNormType)
//                    .eq(PgSettleNorm::getType, type)
//                    .eq(PgSettleNorm::getParentCategoryId, mainId)
//                    .groupBy(PgSettleNorm::getCategoryId)
//                    .list();
//        } else if (StringUtils.isNotBlank(mainId) && StringUtils.isNotBlank(smallId) && StringUtils.isBlank(label)) {
//            list = pgSettleNormService.lambdaQuery()
//                    .eq(PgSettleNorm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(PgSettleNorm::getSettleNormType, settleNormType)
//                    .eq(PgSettleNorm::getType, type)
//                    .eq(PgSettleNorm::getParentCategoryId, mainId)
//                    .eq(PgSettleNorm::getCategoryId, smallId)
//                    .groupBy(PgSettleNorm::getLabel)
//                    .list();
//        }

        return list;
    }

    public List<PgSettleNorm> otherQuery(String type, String label) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        AdminCompanyWechat adminCompanyWechat = adminUser.getAdminCompanyWechat();

        List<PgSettleNorm> list = pgSettleNormService.lambdaQuery()
                    .eq(Objects.nonNull(adminCompanyWechat), PgSettleNorm::getCompanyWechatId, Objects.nonNull(adminCompanyWechat) ? adminCompanyWechat.getCompanyWechatId() : "")
                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.OTHER.getKey())
                    .list();

//        if (StringUtils.isBlank(type)) {
//            list = pgSettleNormService.lambdaQuery()
//                    .eq(PgSettleNorm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.OTHER.getKey())
//                    .groupBy(PgSettleNorm::getType)
//                    .list();
//        } else if (StringUtils.isNotBlank(type) && StringUtils.isBlank(label)) {
//            list = pgSettleNormService.lambdaQuery()
//                    .eq(PgSettleNorm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(PgSettleNorm::getSettleNormType, SettleNormTypeEnum.OTHER.getKey())
//                    .like(PgSettleNorm::getType, type)
//                    .list();
//        }

        return list;
    }

}
