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

import cn.hutool.core.collection.CollUtil;
import com.bonus.sgzb.app.domain.BmAgreementInfo;
import com.bonus.sgzb.app.domain.TmTask;
import com.bonus.sgzb.app.mapper.AppBackApplyAppMapper;
import com.bonus.sgzb.app.service.AppBackApplyService;
import com.bonus.sgzb.base.api.domain.BackApplyInfo;
import com.bonus.sgzb.base.api.domain.MachinePart;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author hay
 * @description 退料申请--app
 * @date 2024/1/22 11:13
 */

@Service
public class AppBackApplyServiceImpl implements AppBackApplyService {

    @Resource
    private AppBackApplyAppMapper appBackApplyMapper;


    @Override
    public List<BmAgreementInfo> getbackUnit(BmAgreementInfo record) {
        return appBackApplyMapper.getbackUnit(record);
    }

    @Override
    public List<BmAgreementInfo> getbackPro(BmAgreementInfo record) {
        return appBackApplyMapper.getbackPro(record);
    }

    @Override
    public int insertTask(TmTask task) {
        return appBackApplyMapper.insertTask(task);
    }

    @Override
    public List<BackApplyInfo> getbackList(BackApplyInfo record) {
        return appBackApplyMapper.getbackList(record);
    }

    @Override
    public int insertTaskAgreement(TmTask task) {
        return appBackApplyMapper.insertTaskAgreement(task);
    }

    @Override
    public int insertBackApply(TmTask task) {
        return appBackApplyMapper.insertBackApply(task);
    }

    @Override
    public List<BackApplyInfo> materialList(BackApplyInfo record) {
        return appBackApplyMapper.materialList(record);
    }

    @Override
    public int upload(BackApplyInfo record) {
        return appBackApplyMapper.upload(record);
    }

    @Override
    public List<BackApplyInfo> view(BackApplyInfo record) {
        return appBackApplyMapper.view(record);
    }

    @Override
    public int del(BackApplyInfo record) {
        return appBackApplyMapper.del(record);
    }

    @Override
    public List<BackApplyInfo> examineList(BackApplyInfo record) {
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        Long companyId = SecurityUtils.getLoginUser().getSysUser().getCompanyId();
        if (roles.contains("admin")) {
            //管理员可以看到所有退料申请
            return appBackApplyMapper.examineList(record);
        }
        List<BackApplyInfo> backApplyInfos = appBackApplyMapper.examineList(record);
        List<BackApplyInfo> backApplyInfoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(backApplyInfos)) {
            if (roles.contains("jjfgs")) {
                for (BackApplyInfo backApplyInfo : backApplyInfos) {
                    if (StringUtils.hasText(backApplyInfo.getCompanyId()) && backApplyInfo.getCompanyId().contains(String.valueOf(companyId))) {
                        backApplyInfoList.add(backApplyInfo);
                    }
                }
            }

            if (roles.contains("tsfgs")) {
                for (BackApplyInfo backApplyInfo : backApplyInfos) {
                    if (StringUtils.hasText(backApplyInfo.getCompanyId()) && backApplyInfo.getCompanyId().contains(String.valueOf(companyId))) {
                        backApplyInfoList.add(backApplyInfo);
                    }
                }
            }
        }
        return backApplyInfoList;

    }

    @Override
    public List<BackApplyInfo> examineView(BackApplyInfo record) {
        List<BackApplyInfo> backApplyInfoList = appBackApplyMapper.examineView(record);
        return getMaTypeDetails(backApplyInfoList);
    }

    private List<BackApplyInfo> getMaTypeDetails(List<BackApplyInfo> view) {
        if (view.size() > 0) {
            for (BackApplyInfo backApplyInfo : view) {
                if ("2".equals(backApplyInfo.getManageType())) {
                    List<MachinePart> machineParts = new ArrayList<>();
                    List<TmTask> typeIds = appBackApplyMapper.getMaTypeDetails(backApplyInfo);
                    typeIds.removeIf(item -> item == null);
                    for (TmTask typeId : typeIds) {
                        MachinePart machinePart = appBackApplyMapper.getMachineParts(typeId);
                        machinePart.setPartNum((typeId.getPartNum() * Double.parseDouble(backApplyInfo.getPreNum())));
                        machineParts.add(machinePart);
                    }
                    backApplyInfo.setMaTypeDetails(machineParts);
                }
            }
        }
        return view;
    }

    @Override
    public int audit(BackApplyInfo record) {
        int num = 0;
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        Long companyId = SecurityUtils.getLoginUser().getSysUser().getCompanyId();
        String userId = String.valueOf(SecurityUtils.getLoginUser().getUserid());
        record.setCreateBy(userId);
        if (roles.contains("admin")) {
            record.setStatus("1");
            appBackApplyMapper.audit(record);
        }
        if (companyId != null && companyId == 101) {
            record.setStatus("1");
            appBackApplyMapper.audit(record);
        }
        if (companyId != null && companyId == 102) {
            record.setStatus("3");
            appBackApplyMapper.audit(record);
        }
        List<Integer> taskIdById = appBackApplyMapper.getTaskIdById(record);
        if (CollUtil.isNotEmpty(taskIdById)) {
            for (Integer taskId : taskIdById) {
                if (taskId == 1 || taskId == 3) {
                    num++;
                }
            }
        }
        if (num == taskIdById.size()) {
            record.setTaskStatus("38");
            appBackApplyMapper.updateTmTask(record);
        }
        return num;
    }

    @Override
    public int refuse(BackApplyInfo record) {
        int num = 0;
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        Long userid = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long companyId = SecurityUtils.getLoginUser().getSysUser().getCompanyId();
        record.setCreateBy(userid.toString());
        if(roles.contains("admin")){
            record.setStatus("2");
            appBackApplyMapper.refuse(record);
        }
        if (companyId != null && companyId == 101) {
            record.setStatus("2");
            appBackApplyMapper.refuse(record);
        }
        if (companyId != null && companyId == 102) {
            record.setStatus("4");
            appBackApplyMapper.refuse(record);
        }
        List<Integer> taskIdById = appBackApplyMapper.getTaskIdById(record);
        if (CollUtil.isNotEmpty(taskIdById)) {
            for (Integer taskId : taskIdById) {
                if (taskId == 2 || taskId == 4) {
                    num++;
                }
            }
        }
        if (num == taskIdById.size()) {
            record.setTaskStatus("101");
            appBackApplyMapper.updateTmTask(record);
        }
        return 1;
    }

}
