package com.bonus.material.record.service.impl;

import com.bonus.common.biz.domain.BmFileInfo;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.ma.domain.DirectApplyInfo;
import com.bonus.material.record.mapper.DerateRecordQueryMapper;
import com.bonus.material.record.mapper.SltRecordQueryMapper;
import com.bonus.material.record.service.IDerateReocrdQueryService;
import com.bonus.material.record.service.ISltReocrdQueryService;
import com.bonus.material.settlement.domain.SltAgreementInfo;
import com.bonus.material.settlement.domain.SltAgreementReduce;
import com.bonus.material.settlement.domain.SltAgreementReduceRecord;
import com.bonus.material.work.domain.SysWorkflowNode;
import com.bonus.material.work.domain.SysWorkflowRecordHistory;
import com.bonus.material.work.mapper.SysWorkflowConfigMapper;
import com.bonus.material.work.mapper.SysWorkflowNodeMapper;
import com.bonus.material.work.mapper.SysWorkflowRecordHistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description 减免记录查询
* @author hay
* @date   2025/2/19 10:47
*/
@Slf4j
@Service
public class DerateRecordQueryServiceImpl implements IDerateReocrdQueryService {
    @Resource
    private DerateRecordQueryMapper mapper;

    @Resource
    private SysWorkflowNodeMapper sysWorkflowNodeMapper;

    @Resource
    private SysWorkflowRecordHistoryMapper sysWorkflowRecordHistoryMapper;


    @Resource
    private SysWorkflowConfigMapper sysWorkflowConfigMapper;


    @Override
    public List<SltAgreementReduceRecord> getList(SltAgreementInfo bean) {
        return mapper.getList(bean);
    }

    @Override
    public List<SltAgreementReduce> getAuditList(SltAgreementInfo bean) {
        Long userId = SecurityUtils.getLoginUser().getUserid();
        bean.setUserId(userId);
        List<SltAgreementReduce> sltAgreementReduceList = mapper.getAuditList(bean);
        try {
            for (SltAgreementReduce sltAgreementReduceNew : sltAgreementReduceList) {
                SltAgreementReduce sltAgreementReducePriceNew = mapper.getAuditPrice(sltAgreementReduceNew.getId());
                sltAgreementReduceNew.setLeasePrice(sltAgreementReducePriceNew.getLeasePrice());
                sltAgreementReduceNew.setLeaseMoney(sltAgreementReducePriceNew.getLeaseMoney());
                sltAgreementReduceNew.setLeaseMoneyAll(sltAgreementReducePriceNew.getLeaseMoneyAll());
                log.info("当前节点为{}",sltAgreementReduceNew.getNodeId());
                SysWorkflowRecordHistory sysWorkflowRecordHistory = sysWorkflowRecordHistoryMapper.getWorkflowRecordHistoryByRecordId(sltAgreementReduceNew.getRecordId());
                sltAgreementReduceNew.setNodeId(sysWorkflowRecordHistory.getNodeId());
                //设置当前节点
                if (1==sltAgreementReduceNew.getNodeSignType() && sysWorkflowRecordHistory.getNodeSort()==1){
                    sltAgreementReduceNew.setNodeId(sysWorkflowRecordHistory.getNextNodeId());
                }
                String configValue = sysWorkflowConfigMapper.selectConfigValueByNodeId(sltAgreementReduceNew.getNodeId());
                SysWorkflowNode sysWorkflowNode = sysWorkflowNodeMapper.selectSysWorkflowNodeById(sltAgreementReduceNew.getNodeId());
                sltAgreementReduceNew.setConfigValue(configValue);
                //判断当前节点是否审核
                sysWorkflowRecordHistory.setRecordId(sltAgreementReduceNew.getRecordId());
                sysWorkflowRecordHistory.setNodeId(sltAgreementReduceNew.getNodeId());
                List<SysWorkflowRecordHistory> sysWorkflowRecordHistoryList  = sysWorkflowRecordHistoryMapper.seleteSysWorkflowRecordHistory(sysWorkflowRecordHistory);
                //判断是或签还是会签0:或签  1:会签
                if (0==sysWorkflowNode.getNodeSignType()){
                    //当前节点已经审核需要修改节点值
                    if (sysWorkflowRecordHistoryList.get(0)!=null){
                        String configValueNew = sysWorkflowConfigMapper.selectConfigValueByNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
                        sltAgreementReduceNew.setNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
                        sltAgreementReduceNew.setConfigValue(configValueNew);
                    }
                }else {
                    String[] ConfigValue = configValue.split(",");
                    List<String> filteredList = Arrays.stream(ConfigValue).collect(Collectors.toList());
                    for (SysWorkflowRecordHistory sysWorkflowRecordHistoryNew : sysWorkflowRecordHistoryList) {
                        if (sysWorkflowRecordHistoryNew != null) {
                            // 过滤数组元素
                            filteredList = filteredList.stream()
                                    .filter(value -> value != null && !value.equals(sysWorkflowRecordHistoryNew.getCreateBy()))
                                    .collect(Collectors.toList());
                            sltAgreementReduceNew.setConfigValue(filteredList.toString());
                        }
                    }
                    //判断是否这个节点的所有人都审核
                    if (filteredList.size() == 0) {
                        String configValueNew = sysWorkflowConfigMapper.selectConfigValueByNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
                        sltAgreementReduceNew.setNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
                        sltAgreementReduceNew.setConfigValue(configValueNew);
                    } else {
                        if (sysWorkflowRecordHistoryList.size() > 0) {
                            sltAgreementReduceNew.setNodeId(sltAgreementReduceNew.getNodeId());
                            sltAgreementReduceNew.setConfigValue(filteredList.toString());
                        }
                    }
                }
            }

        }catch (Exception e){
            log.info("查询结果出错{}",e.getMessage());
            throw new ServiceException("查询结果出错");
        }
        return sltAgreementReduceList;
    }

    @Override
    public int delData(Long id) {
        try {
            mapper.delData(id);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public SltAgreementReduce getInfo(Long id, String keyWord) {
        try {
            //先根据Id查询apply信息
            SltAgreementReduce info = mapper.getInfo(id);
            if (info != null && info.getId() != null) {
                //查询附件
                List<BmFileInfo> bmFileInfos= mapper.getBmFileInfos(info);
                if (bmFileInfos != null && bmFileInfos.size() > 0){
                    info.setBmFileInfos(bmFileInfos);
                }
                //查询详情
                List<SltAgreementReduce> detailList = mapper.getDetailList(id);
                if (detailList != null && detailList.size() > 0){
                    info.setDetailList(detailList);
                }
            }
            return info;
        } catch (Exception e) {
            return new SltAgreementReduce();
        }
    }
}
