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

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.ma.domain.*;
import com.bonus.material.ma.mapper.DirectRotationMapper;
import com.bonus.material.ma.service.DirectRotationService;
import com.bonus.material.ma.service.WorkSiteDirectManageService;
import com.bonus.material.settlement.domain.SltAgreementInfo;
import com.bonus.material.work.domain.SysWorkflowRecord;
import com.bonus.material.work.domain.SysWorkflowRecordHistory;
import com.bonus.material.work.mapper.SysWorkflowRecordHistoryMapper;
import com.bonus.material.work.mapper.SysWorkflowRecordMapper;
import com.bonus.material.work.service.SysWorkflowRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.bonus.common.biz.enums.TmTaskTypeEnum.TM_TASK_DIRECT;

/**
 * @author hay
 * @description 直转申请
 * @date 2025/2/17 19:34
 */
@Service(value = "directRotationService")
@Slf4j
public class DirectRotationImpl implements DirectRotationService {

    @Resource
    private DirectRotationMapper mapper;

    @Resource
    private SysWorkflowRecordMapper wfMapper;

    @Resource
    private SysWorkflowRecordHistoryMapper hisMapper;

    @Resource(name = "workSiteDirectManageService")
    private WorkSiteDirectManageService workSiteDirectManageService;

    @Resource
    private SysWorkflowRecordService sysWorkflowRecordService;

    @Override
    public List<SltAgreementInfo> getUseringData(SltAgreementInfo sltAgreementInfo) {
        return mapper.getUseringData(sltAgreementInfo);
    }

    @Override
    public List<DirectApplyExportInfo> getList(DirectApplyInfo directApplyInfo) {
        return mapper.getList(directApplyInfo);
    }

    @Override
    public AjaxResult submit(DirectApplyInfoDetails directApplyInfoDetails) {
        int id = 0;
        String code ="";
        if (directApplyInfoDetails != null) {
            if (directApplyInfoDetails.getDirectApplyInfo() != null) {
                DirectApplyInfo directApplyInfos = directApplyInfoDetails.getDirectApplyInfo();
                if (directApplyInfos.getBackAgreementId()==directApplyInfos.getLeaseAgreementId()){
                    return AjaxResult.error("申请失败，不能转入同单位同工程");
                }
                 code = workSiteDirectManageService.genderLeaseCode();
                directApplyInfos.setCode(code);
                directApplyInfos.setCreateTime(new Date());
                List<String> urls = new ArrayList<>();
                if (directApplyInfoDetails.getDirectApplyInfo().getDirUrls() != null) {
                   List<DirectApplyUrl> dirUrls = directApplyInfoDetails.getDirectApplyInfo().getDirUrls();
                   for (DirectApplyUrl dirUrl : dirUrls){
                       urls.add(dirUrl.getUrl());
                   }
                   //将urls所有内容用逗号拼接
                    directApplyInfos.setDirUrl(String.join(",", urls));
                }
//                if (directApplyInfoDetails.getDirectApplyInfo().getDirUrls() != null && directApplyInfoDetails.getDirectApplyInfo().getDirUrls().length > 0){
//                    //使用逗号拼接
//                    directApplyInfos.setDirUrl(String.join(",", directApplyInfoDetails.getDirectApplyInfo().getDirUrls()));
//                }
                directApplyInfos.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                int i = workSiteDirectManageService.saveDirectApplyInfo(directApplyInfos);
                if (i < 0) {
                    return AjaxResult.error("保存失败");
                }
                id = directApplyInfos.getId();
            } else {
                return AjaxResult.error("保存失败");
            }

            if (directApplyInfoDetails.getDirectApplyInfoDetails() != null) {
                List<DirectApplyDetails> directApplyDetails = directApplyInfoDetails.getDirectApplyInfoDetails();
                for (DirectApplyDetails directApplyDetail : directApplyDetails) {
                    directApplyDetail.setDirectId(id);
                    int i = workSiteDirectManageService.saveDirectApplyDetails(directApplyDetail);
                    if (i < 0) {
                        return AjaxResult.error("保存失败");
                    }
                }
            }
        } else {
            return AjaxResult.error("保存失败");
        }
        sysWorkflowRecordService.addSysWorkflowRecord(id,code,TM_TASK_DIRECT.getTaskTypeId());
        //initAuditDir(id,code);


        return AjaxResult.success("保存成功");
    }

    private void initAuditDir(int id, String code) {

        //1 生成直转审核节点任务
        SysWorkflowRecord wf = new SysWorkflowRecord();
        wf.setTaskId(id);
        wf.setTaskType(TM_TASK_DIRECT.getTaskTypeId());
        wf.setTaskCode(code);
        wf.setWorkflowId(15);
        wfMapper.addSysWorkflowRecord(wf);
        //2 生成直转审核人
        SysWorkflowRecordHistory his = new SysWorkflowRecordHistory();

        his.setRecordId(wf.getId());
        his.setNodeId(9);
        his.setNextNodeId(10);
        his.setIsAccept(1);
        his.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        his.setCreateTime(new Date());
        hisMapper.addSysWorkflowRecordHistory(his) ;

    }


    @Override
    public AjaxResult edit(DirectApplyInfoDetails directApplyInfoDetails) {
        if (directApplyInfoDetails != null) {
            if (directApplyInfoDetails.getDirectApplyInfo() != null && directApplyInfoDetails.getDirectApplyInfo().getId() != null) {
                DirectApplyInfo directApplyInfos = directApplyInfoDetails.getDirectApplyInfo();
                if (directApplyInfos.getBackAgreementId()==directApplyInfos.getLeaseAgreementId()){
                    return AjaxResult.error("申请失败，不能转入同单位同工程");
                }
                directApplyInfos.setUpdateTime(new Date());
                List<String> urls = new ArrayList<>();
                if (directApplyInfoDetails.getDirectApplyInfo().getDirUrls() != null) {
                    List<DirectApplyUrl> dirUrls = directApplyInfoDetails.getDirectApplyInfo().getDirUrls();
                    for (DirectApplyUrl dirUrl : dirUrls){
                        urls.add(dirUrl.getUrl());
                    }
                    //将urls所有内容用逗号拼接
                    directApplyInfos.setDirUrl(String.join(",", urls));
                    //修改info表
                    int i = mapper.updateDirectApplyInfo(directApplyInfos);
                    //删除详情表内容
                    int i1 = mapper.deleteDirectApplyDetails(directApplyInfos.getId());
                    if (directApplyInfoDetails.getDirectApplyInfoDetails() != null) {
                        List<DirectApplyDetails> directApplyDetails = directApplyInfoDetails.getDirectApplyInfoDetails();
                        for (DirectApplyDetails directApplyDetail : directApplyDetails) {
                            directApplyDetail.setDirectId(directApplyInfos.getId());
                            int i2 = workSiteDirectManageService.saveDirectApplyDetails(directApplyDetail);
                        }
                    }
                    return AjaxResult.success("修改成功");
                }
            } else {
                return AjaxResult.error("修改失败");
            }
        } else {
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }

    @Override
    public DirectApplyInfo getInfoById(SltAgreementInfo sltAgreementInfo) {
        DirectApplyInfo directApplyInfo = mapper.getInfoById(sltAgreementInfo);
        if (directApplyInfo.getDirUrl()!= null){
            //用逗号分割放入数组
            String[] split = directApplyInfo.getDirUrl().split(",");
            List<DirectApplyUrl> list1 = new ArrayList<>();
            for (String s : split) {
                list1.add(new DirectApplyUrl(s));
            }
            directApplyInfo.setDirUrls(list1);
        }
        List<DirectApplyDetails> detailById = mapper.getDetailById(sltAgreementInfo);
        directApplyInfo.setDirectApplyDetails(detailById);
        return directApplyInfo;
    }

    @Override
    public DirectApplyInfo getInfoApp(SltAgreementInfo sltAgreementInfo) {
        DirectApplyInfo directApplyInfo = mapper.getInfoById(sltAgreementInfo);
        if (directApplyInfo != null){
            if (sltAgreementInfo.getAgreementId()!=directApplyInfo.getLeaseAgreementId()){
                return new DirectApplyInfo();
            }
        }
        if (directApplyInfo.getDirUrl()!= null){
            //用逗号分割放入数组
            String[] split = directApplyInfo.getDirUrl().split(",");
            List<DirectApplyUrl> list1 = new ArrayList<>();
            for (String s : split) {
                list1.add(new DirectApplyUrl(s));
            }
            directApplyInfo.setDirUrls(list1);
        }
        List<DirectApplyDetails> detailById = mapper.getDetailById(sltAgreementInfo);
        directApplyInfo.setDirectApplyDetails(detailById);
        return directApplyInfo;
    }

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

    @Override
    public AjaxResult getTypeNameList(SltAgreementInfo bean) {
        List<SltAgreementInfo> list =new ArrayList<>();
        try {
            list = mapper.getTypeNameList(bean);
        } catch (Exception e) {
            return AjaxResult.success(new ArrayList());
        }
        return AjaxResult.success(list);
    }

}
