package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.domain.RepairApplyRecord;
import com.bonus.sgzb.base.domain.RepairPartDetails;
import com.bonus.sgzb.base.domain.RepairTask;
import com.bonus.sgzb.base.domain.RepairTaskDetails;
import com.bonus.sgzb.base.domain.vo.DictVo;
import com.bonus.sgzb.base.mapper.RepairMapper;
import com.bonus.sgzb.base.service.RepairService;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.system.api.domain.SysUser;
import com.bonus.sgzb.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author c liu
 * @date 2023/12/11
 */
@Service("RepairService")
public class RepairServiceImpl implements RepairService {
    @Autowired
    private RepairMapper mapper;


    @Override
    public List<RepairTask> getRepairTaskList(RepairTask bean) {
        List<RepairTask> repairTaskList = mapper.getRepairTaskList(bean);
        return repairTaskList;
    }

    @Override
    public List<RepairTask> exportRepairTaskList(RepairTask bean) {
        return mapper.exportRepairTaskList(bean);
    }

    @Override
    public List<RepairTaskDetails> getRepairMaTypeList(RepairTaskDetails bean) {
        Long companyId = SecurityUtils.getLoginUser().getSysUser().getCompanyId();
        bean.setCompanyId(companyId);
        List<RepairTaskDetails> repairMaTypeList = mapper.getRepairMaTypeList(bean);
        return repairMaTypeList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult submitRepairApply(RepairApplyRecord bean) {
        RepairTaskDetails details = mapper.getById(bean.getId());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bean.setCreateBy(loginUser.getUserid());
        List<RepairPartDetails> partList = bean.getPartList();
        BigDecimal sfCosts = new BigDecimal("0");
        String nbType = "1";
        String fcType = "2";
        String sfPart = "1";
        String bsfPart = "0";
        if (partList != null && partList.size() > 0) {
            bean.setRepairNum(partList.get(0).getRepairNum());
            bean.setRepairer(partList.get(0).getRepairer());
        }
        switch (bean.getRepairType()) {
            case "1": {
                int repairNum = details.getRepairedNum() + bean.getRepairNum();
                int num = repairNum + details.getScrapNum();
                if (num > details.getRepairNum()) {
                    throw new ServiceException("维修数量大于维修总量");
                }
                mapper.updateRepairedNum(bean.getId(), repairNum, bean.getRepairer(), loginUser.getUserid());
                break;
            }
            case "2": {
                int repairNum = details.getRepairedNum() + bean.getRepairNum();
                int num = repairNum + details.getScrapNum();
                if (num > details.getRepairNum()) {
                    throw new ServiceException("维修数量大于维修总量");
                }
                mapper.updateRepairedNumTwo(bean.getId(), repairNum, loginUser.getUserid());
                break;
            }
            case "3": {
                int scrapNum = details.getScrapNum() + bean.getScrapNum();
                int num = scrapNum + details.getRepairedNum();
                if (num > details.getRepairNum()) {
                    throw new ServiceException("维修数量大于维修总量");
                }
                mapper.updateScrapNum(bean.getId(), scrapNum, loginUser.getUserid());
                break;
            }
            default:
                break;
        }
        if (partList != null && partList.size() > 0) {
            if (nbType.equals(bean.getRepairType())) {
                for (RepairPartDetails partDetails : partList) {
                    if (partDetails.getPartId() != null) {
                        // 有维修配件时
                        if (partDetails.getPartCost() == null || partDetails.getPartCost().isEmpty()) {
                            partDetails.setPartCost("0");
                        }
                        partDetails.setTaskId(bean.getTaskId());
                        partDetails.setMaId(bean.getMaId());
                        partDetails.setTypeId(bean.getTypeId());
                        partDetails.setCreateBy(loginUser.getUserid());
                        partDetails.setCompanyId(bean.getCompanyId());
                        // 根据partid 找到配件单价
                        String partPrice = mapper.selectPartPrice(partDetails.getPartId());
                        partDetails.setPartCost(partPrice);
                        mapper.addPart(partDetails);
                        bean.setPartPrice(partDetails.getPartCost());
                        bean.setPartId(partDetails.getPartId().intValue());
                        bean.setPartNum(partDetails.getPartNum());
                        bean.setRepairContent(partDetails.getRepairContent());
                        bean.setPartType(partDetails.getPartType());
                        mapper.addRecord(bean);
                    } else {
                        // 不选维修配件时
                        mapper.addRecord(bean);
                    }
                }
            }
            if (fcType.equals(bean.getRepairType())) {
                bean.setPartName(partList.get(0).getPartName());
                bean.setPartType(partList.get(0).getPartType());
                bean.setRepairContent(partList.get(0).getRepairContent());
                if (partList.get(0).getSupplierId() == null) {
                    throw new ServiceException("请选择返厂厂家");
                } else {
                    bean.setSupplierId(partList.get(0).getSupplierId());
                }
                if (partList.get(0).getPartPrice() == null || partList.get(0).getPartPrice().isEmpty()) {
                    bean.setPartPrice("0");
                } else {
                    bean.setPartPrice(partList.get(0).getPartPrice());
                }
                bean.setPartNum(partList.get(0).getPartNum());
                mapper.addRecord(bean);
            }
            for (RepairPartDetails partDetails : partList) {
                if (StringUtils.isNotBlank(partDetails.getPartCost())) {
                    BigDecimal partCost = new BigDecimal(partDetails.getPartCost());
                    BigDecimal partNumber = new BigDecimal(partDetails.getPartNum());
                    sfCosts = sfCosts.add(partCost.multiply(partNumber));
                }
            }

            if (!"0".equals(sfCosts.toString())) {
                mapper.addRepairCost(bean, sfCosts, sfPart);
            }
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult fastRepairApply(List<RepairTaskDetails> list) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (RepairTaskDetails bean : list) {
            int repairedNum = bean.getRepairNum() - bean.getRepairedNum() - bean.getScrapNum();
            if (repairedNum <= 0) {
                throw new ServiceException("选中的数据中包含待维修数量为0的机具，请重新选择");
            }
        }
        for (RepairTaskDetails bean : list) {
            int repairedNum = bean.getRepairNum() - bean.getRepairedNum() - bean.getScrapNum();
            RepairApplyRecord partDetails = new RepairApplyRecord();
            partDetails.setTaskId(bean.getTaskId());
            partDetails.setMaId(bean.getMaId());
            partDetails.setTypeId(bean.getTypeId());
            partDetails.setRepairNum(repairedNum);
            partDetails.setRepairType("1");
            partDetails.setCreateBy(loginUser.getUserid());
            partDetails.setCompanyId(bean.getCompanyId());
            mapper.addRecord(partDetails);
            int i = repairedNum + bean.getRepairedNum();
            mapper.updateRepairedNumTwo(bean.getId(), i, loginUser.getUserid());
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult endRepairTask(List<RepairTask> taskList) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (RepairTask task : taskList) {
            int i = mapper.getUnFinish(task);
            if (i > 0) {
                throw new ServiceException("选中的数据中包含维修未完成的，请完成维修再进行提交审核");
            }
        }
        int i = mapper.updateTaskStatus(taskList, loginUser.getUserid());
        for (RepairTask task : taskList) {
            task.setCreateBy(loginUser.getUserid());
            Long agreementId = mapper.getAgreementId(task);
            List<RepairTaskDetails> detailsList = mapper.getDetailsListByTaskId(task);
            mapper.addTask(task);
            for (RepairTaskDetails details : detailsList) {
                details.setCreateBy(loginUser.getUserid());
                details.setTaskId(task.getTaskId());
                mapper.addAuditDetails(details);
            }
            task.setAgreementId(agreementId);
            mapper.createAgreementTask(task);
        }
        return AjaxResult.success();
    }

    @Override
    public List<DictVo> getDicSelect(String value) {
        return mapper.getDicSelect(value);
    }


    @Override
    public int completeRepair(ArrayList<Long> ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return mapper.completeRepair(ids, loginUser.getUserid());
    }

    @Override
    public List<SysUser> selectUserList() {
        return mapper.selectUserList();
    }

}
