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

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.ExamineProjectConfigVO;
import com.gree.mall.manager.bean.workorder.ExamineProjectVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.enums.workorder.ExamineProjectTypeEnum;
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.PgExamineProject;
import com.gree.mall.manager.plus.entity.PgExamineProjectConfig;
import com.gree.mall.manager.plus.entity.PgOrderBase;
import com.gree.mall.manager.plus.entity.PgSettleOrder;
import com.gree.mall.manager.plus.service.PgExamineProjectConfigService;
import com.gree.mall.manager.plus.service.PgExamineProjectService;
import com.gree.mall.manager.plus.service.PgOrderBaseService;
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.math.BigDecimal;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class ExamineProjectConfigLogic {

    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final PgExamineProjectConfigService pgExamineProjectConfigService;
    private final PgExamineProjectService pgExamineProjectService;
    private final PgOrderBaseService pgOrderBaseService;

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

        return page;
    }

    public PgExamineProjectConfig configDetail(String id) {
        return pgExamineProjectConfigService.getById(id);
    }

    @Transactional
    public void configAdd(PgExamineProjectConfig config) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        checkParams(config);
        if (Objects.nonNull(adminUser.getAdminCompanyWechat())) {
            config.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
            config.setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName());
        }

        PgExamineProjectConfig one = pgExamineProjectConfigService.lambdaQuery()
                .eq(PgExamineProjectConfig::getCompanyWechatId, config.getCompanyWechatId())
                .eq(PgExamineProjectConfig::getType, config.getType())
                .eq(PgExamineProjectConfig::getName, config.getName())
                .one();
        if (Objects.nonNull(one)) {
            throw new RemoteServiceException("已有相同考核项目,添加失败");
        }

        config.setId(null)
                .insert();
    }

    @Transactional
    public void configEdit(PgExamineProjectConfig config) {
        checkParams(config);

        PgExamineProjectConfig one = pgExamineProjectConfigService.lambdaQuery()
                .eq(PgExamineProjectConfig::getCompanyWechatId, config.getCompanyWechatId())
                .eq(PgExamineProjectConfig::getType, config.getType())
                .eq(PgExamineProjectConfig::getName, config.getName())
                .ne(PgExamineProjectConfig::getId, config.getId())
                .one();
        if (Objects.nonNull(one)) {
            throw new RemoteServiceException("已有相同考核项目,编辑失败");
        }

        config.updateById();
    }

    private void checkParams(PgExamineProjectConfig config) {
        if (StringUtils.isBlank(config.getType())) {
            throw new RemoteServiceException("考核类型不能为空");
        }
        if (StringUtils.isBlank(config.getName())) {
            throw new RemoteServiceException("考核项目不能为空");
        }
        if (Objects.isNull(config.getAmount())) {
            throw new RemoteServiceException("考核金额不能为空");
        }
    }

    public void configDelete(String id) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        pgExamineProjectConfigService.lambdaUpdate()
                .eq(PgExamineProjectConfig::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(PgExamineProjectConfig::getId, id)
                .remove();
    }

    public IPage<ExamineProjectVO> list(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, ExamineProjectVO.class, commonLogic.getAdminUser());


        IPage<ExamineProjectVO> page = commonMapper.examineProjectList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean, adminUser.getType());

        return page;
    }

    public PgExamineProject detail(String id) {
        return pgExamineProjectService.getById(id);
    }

    public void add(PgExamineProject project) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (Objects.nonNull(adminUser.getAdminCompanyWechat())) {
            project.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
            project.setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName());
        }

        if (StringUtils.isBlank(project.getWebsitId()) || StringUtils.isBlank(project.getWebsitName())) {
            throw new RemoteServiceException("网点不能为空");
        }
        if (StringUtils.isBlank(project.getWorkerId()) || StringUtils.isBlank(project.getWorkerName())) {
            throw new RemoteServiceException("工程师不能为空");
        }
        if (StringUtils.isBlank(project.getExamineProjectId())) {
            throw new RemoteServiceException("考核项目不能为空");
        }
        PgExamineProjectConfig config = pgExamineProjectConfigService.getById(project.getExamineProjectId());

        project.setExamineProjectName(config.getName())
                .setExamineProjectType(config.getType())
                .setExamineProjectAmount(config.getAmount());

        project.insert();

        PgOrderBase orderBase = pgOrderBaseService.getById(project.getOrderBaseId());

        // 插入结算单
        PgSettleOrder order = new PgSettleOrder();
        order.setCompanyWechatId(project.getCompanyWechatId());
        order.setCompanyWechatName(project.getCompanyWechatName());
        order.setWebsitId(project.getWebsitId());
        order.setWebsitName(project.getWebsitName());
        order.setOrderBaseId(project.getOrderBaseId());
        order.setWorkerId(project.getWorkerId());
        order.setWorkerName(project.getWorkerName());
        order.setWorkerMobile(project.getMobile());
        order.setWorkerIdcard(project.getIdcard());
        if (Objects.nonNull(orderBase)) {
            order.setCustomerName(orderBase.getUserName());
            order.setCustomerMobile(orderBase.getUserMobile());
            order.setCustomerAddress(orderBase.getAddress());
            order.setOrderStatus(orderBase.getOrderStatus());
//            order.setWorkerId(orderBase.getWorkerId());
//            order.setWorkerName(orderBase.getWorkerName());
//            order.setWorkerMobile(orderBase.getWorkerMobile());
//            order.setWorkerIdcard(orderBase.getWorkerIdcard());
            order.setOrderCreateTime(orderBase.getCreateTime());
            order.setWorkerEndTime(orderBase.getOverTime());
            order.setOrderType(orderBase.getOrderType());
            order.setOrderTypeText(orderBase.getOrderTypeText());
            order.setOrderSmallType(orderBase.getOrderSmallType());
            order.setOrderSmallTypeText(orderBase.getOrderSmallTypeText());
            order.setOrderSource(orderBase.getSource());
            order.setOrderChannelId(orderBase.getOrderChannelId());
            order.setOrderChannel(orderBase.getOrderChannelText());
        }

        BigDecimal direct = project.getExamineProjectType().equals(ExamineProjectTypeEnum.REWARD.getKey()) ? BigDecimal.ONE : new BigDecimal("-1");
        order.setSettleOrderType(SettleNormTypeEnum.EXAMINE.getKey());
        order.setExamineProjectId(project.getId());
        order.setExamineProject(project.getExamineProjectName());
        order.setExamineType(project.getExamineProjectType());
        order.setSettleNum(0);
        order.setSettleAmount(project.getExamineProjectAmount().multiply(direct));
        order.setRemark(project.getRemark());

        order.insert();
    }
}
