package com.jwipc.spe.logic.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.constant.SequenceConst;
import com.jwipc.spe.convert.QmNoticeOrderConvert;
import com.jwipc.spe.convert.SupAppraiseDimensionConvert;
import com.jwipc.spe.dto.QmNoticeOrderDTO;
import com.jwipc.spe.dto.QmRectifiedMeasuresDTO;
import com.jwipc.spe.entity.QmNoticeOrder;
import com.jwipc.spe.entity.QmNoticeOrderExamineLog;
import com.jwipc.spe.entity.QmRectifiedMeasures;
import com.jwipc.spe.logic.QmNoticeOrderLogic;
import com.jwipc.spe.model.po.QmNoticeOrderPO;
import com.jwipc.spe.model.query.QmNoticeOrderQuery;
import com.jwipc.spe.service.IQmNoticeOrderExamineLogService;
import com.jwipc.spe.service.IQmNoticeOrderService;
import com.jwipc.spe.service.IQmRectifiedMeasuresService;
import com.jwipc.spe.service.ISupAppraiseDimensionService;
import com.jwipc.sys.client.SequenceClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Al
 * @create 2024-02-28 16:08
 * 质量整改
 */
@Service
@RequiredArgsConstructor
public class QmNoticeOrderLogicImpl implements QmNoticeOrderLogic {
    private final ISupAppraiseDimensionService supAppraiseDimensionService;
    private final IQmNoticeOrderService qmNoticeOrderService;
    private final SupAppraiseDimensionConvert supAppraiseDimensionConvert;
    private final QmNoticeOrderConvert qmNoticeOrderConvert;
    private final SequenceClient sequenceClient;
    private final IQmNoticeOrderExamineLogService qmNoticeOrderExamineLogService;
    private final IQmRectifiedMeasuresService qmRectifiedMeasuresService;

    //列表页面数据
    @Override
    public PageView findByPage(QmNoticeOrderQuery query) {
        final Page<QmNoticeOrder> dbPage = PageUtil.toPage(query);

        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS), dbPage.getTotal());
    }
    //结案数据
    @Override
    public PageView findByPageEnd(QmNoticeOrderQuery query) {
        final Page<QmNoticeOrder> dbPage = PageUtil.toPage(query);

        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.findPageByQueryEnd(dbPage, query);

        return PageUtil.getPV(qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS), dbPage.getTotal());
    }
//    @Override
//    public List<QmNoticeOrderMerge> findFatherMergeByPage(QmNoticeOrderMerge qmNoticeOrderMerge) {
//       // final Page<QmNoticeOrder> dbPage = PageUtil.toPage(qmNoticeOrder);
//        qmNoticeOrderMerge.setMainNoticeOrderId(qmNoticeOrderMerge.getId());
//        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.findFatherMergeByPage(qmNoticeOrderMerge);
//
//        return PageUtil.getPV(qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS),qmNoticeOrderPOS.size());
//    }
    //列表主数据
    @Override
    public List<QmNoticeOrderDTO> listAll(QmNoticeOrderQuery query) {
        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.findPageByQuery(null, query);
        return qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS);
    }
    //质量发起管理新增编辑的保存
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(QmNoticeOrderDTO param) {
        try {
        //保存
        //新建
        if (Objects.isNull(param.getId())) {
            //param.setId(null);
            param.setNoticeOrderCode(sequenceClient.createSequenceNo(SequenceConst.NOTICE_ORDER_CODE.getCode()));
            //param.setNoticeOrderCode(sequenceClient.createSequenceNo(SequenceConst.DEDUCTION_CODE.getCode()));
            param.setCreateTime(LocalDateTime.now());
            param.setCreateUserId(LoginUtil.getUserId());
            param.setCreateUserName(LoginUtil.getCurrentUser().getName());
            param.setStatus(0);
            param.setNoticeType(2);
            param.setFollowingStatus(1);
        } else {


            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUserId(LoginUtil.getUserId());
            param.setUpdateUserName(LoginUtil.getCurrentUser().getName());
        }

            qmNoticeOrderService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity(param));
//        if (StringUtils.equals(publishFlag, "1")) {
//            release(param.getId());
//        }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    //供应商确认保存
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateSup(QmNoticeOrderDTO param) {

            final CurrentUserDTO shiroUser = LoginUtil.getCurrentUser();
            final QmNoticeOrder qmNoticeOrder = qmNoticeOrderService.getById(param.getId());
            //QmRectifiedMeasures qmRectifiedMeasures = new QmRectifiedMeasures();
            //NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
            //param.setId(param.getId());
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUserId(shiroUser.getUserId());
            param.setUpdateUserName(shiroUser.getName());
            param.setSupAgreeTime(LocalDateTime.now());
//            if (param.getEmergencyHandling() != 3 && param.getDefectiveBatches() == 0) {
//                param.setEmergencyHandling(param.getEmergencyHandling());
//                param.setRiskAssessment(param.getRiskAssessment());
//            } else if (param.getEmergencyHandling() == 3 && param.getDefectiveBatches() == 0) {
//                param.setEmergencyHandling(param.getEmergencyHandling());
//                param.setRiskAssessment(param.getRiskAssessment());
//                param.setVisitingName(param.getVisitingName());
//                param.setVisitingCall(param.getVisitingCall());
//                param.setVisitingPosition(param.getVisitingPosition());
//            } else if (param.getEmergencyHandling() == 0 && param.getDefectiveBatches() != 0) {
//                param.setDefectiveBatches(param.getDefectiveBatches());
//                param.setVisitingName(param.getVisitingName());
//                param.setVisitingCall(param.getVisitingCall());
//                param.setVisitingTime(param.getVisitingTime());
//            }
            //是否同意整改
            param.setSupAgreeRma(param.getSupAgreeRma());
            param.setMergeFlag(0);
//            if (param.getMergeFlag() == 1) {
//                //如果归并，设为父级; 0:初始 1:作为父亲 2:作为儿子
//                param.setMergeFlag(1);
//                //归并确认标识；0-未归并；1-归并待确认；2-归并已确认；3-归并移除
//                param.setMergeConfirmFlag(1);
//            } else {
//                param.setMergeFlag(0);
//            }
            //如果拒绝，插入拒绝说明0:拒绝
            param.setSupAgreeRma(1);
            if (param.getSupAgreeRma() == 0) {
                param.setSupDisagreeExplain(param.getSupDisagreeExplain());
                //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
                param.setStatus(3);
            } else {
                //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
                param.setStatus(10);
                param.setIscAcceptDissent(1);
                param.setIscAcceptDissentTime(LocalDateTime.now());
            }
            param.setMaterialName(param.getMaterialName());
            param.setPlanTime(param.getPlanTime());

            qmNoticeOrderService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity(param));
            //qmRectifiedMeasures.setId(null);
//            qmRectifiedMeasures.setNoticeOrderId(param.getId());
//            qmRectifiedMeasures.setCreateUserId(shiroUser.getUserId());
//            qmRectifiedMeasures.setCreateUserName(shiroUser.getName());
            param.setNoticeOrderId(param.getId());
            param.setCreateUserId(shiroUser.getUserId());
            param.setCreateUserName(shiroUser.getName());
            //qmRectifiedMeasuresService.save(qmRectifiedMeasures);
             qmRectifiedMeasuresService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity1(param));
            //qmRectifiedMeasuresService.saveOrUpdate(qmRectifiedMeasures);
            //qmRectifiedMeasuresService.save(qmRectifiedMeasures);
           // noticeOrderMapper.editEntity(param);
            //新建日志
//            QmNoticeOrderExamineLog qmNoticeOrderExamineLog = new QmNoticeOrderExamineLog();
//            qmNoticeOrderExamineLog.setNoticeOrderId(param.getId());
//            qmNoticeOrderExamineLog.setExamineUserCode(shiroUser.getLoginName());
//            qmNoticeOrderExamineLog.setExamineUserName(shiroUser.getName());
//            //如果拒绝，插入拒绝说明
//            if (param.getSupAgreeRma() == 0) {
//                //设为拒绝
//                qmNoticeOrderExamineLog.setExamineStatus(2);
//                qmNoticeOrderExamineLog.setExamineView(param.getSupDisagreeExplain());
//
//            } else {
//                //设为同意
//                qmNoticeOrderExamineLog.setExamineStatus(1);
//                qmNoticeOrderExamineLog.setExamineView("同意");
//            }
//            qmNoticeOrderExamineLog.setExamineUserId(shiroUser.getUserId());
//            qmNoticeOrderExamineLog.setExamineTime(LocalDateTime.now());
//            qmNoticeOrderExamineLog.setExamineNodeName("供方不良确认");
//            qmNoticeOrderExamineLog.setExamineNodeCode("2");
//            qmNoticeOrderExamineLogService.saveOrUpdate(qmNoticeOrderExamineLog);
            //noticeOrderExamineLogMapper.addEntity(qmNoticeOrderExamineLog);
            //新建归并关系
            //NoticeOrderMergeFormMap noticeOrderMergeFormMap = new NoticeOrderMergeFormMap();
            //归并处理标识 0:初始 1:作为父亲 2:作为儿子

//            if (mergeFlag == 1) {
//                noticeOrderMergeFormMap.put("merge_flag", 1);
//                noticeOrderMergeFormMap.put("main_notice_order_id", id);
//                noticeOrderMergeFormMap.put("notice_order_id", id);
//                noticeOrderMergeFormMap.put("supplier_code", shiroUser.getLoginName());
//                noticeOrderMergeMapper.addEntity(noticeOrderMergeFormMap);
//            }
            //如果同意归并
//            if (mergeFlag == 1) {
//                List<String> idsList = Lists.newArrayList();
//                idsList.addAll(Arrays.asList(StringUtils.split(ids, ",")));
//                for (String noticeOrderId : idsList) {
//                    NoticeOrderFormMap noticeOrderFormMapSon = new NoticeOrderFormMap();
//                    noticeOrderFormMapSon.put("id", noticeOrderId);
//                    noticeOrderFormMapSon.put("update_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                    noticeOrderFormMapSon.put("update_user_id", shiroUser.getUserId());
//                    noticeOrderFormMapSon.put("update_user_name", shiroUser.getLoginName());
//                    if (emergency_handling != 0 && defective_batches == 0) {
//                        noticeOrderFormMap.put("emergency_handling", emergency_handling);
//                        noticeOrderFormMap.put("risk_assessment", risk_assessment);
//                    } else if (emergency_handling == 0 && defective_batches != 0) {
//                        noticeOrderFormMap.put("defective_batches", defective_batches);
//                        noticeOrderFormMap.put("visiting_name", visiting_name);
//                        noticeOrderFormMap.put("visiting_call", visiting_call);
//                        noticeOrderFormMap.put("visiting_time", visiting_time);
//                    }
//                    //是否同意整改
//                    noticeOrderFormMapSon.put("sup_agree_rma", supAgreeRma);
//                    //如果拒绝，插入拒绝说明
//                    if (supAgreeRma == 0) {
//                        noticeOrderFormMapSon.put("sup_disagree_explain", supDisagreeExplain);
//                        noticeOrderFormMapSon.put("status", 3);
//                    } else {
//                        //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
//                        noticeOrderFormMapSon.put("status", 4);
//                        noticeOrderFormMapSon.put("isc_accept_dissent", 1);
//                        noticeOrderFormMapSon.put("isc_accept_dissent_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                    }
//                    //设为儿子; 0:初始 1:作为父亲 2:作为儿子
//                    noticeOrderFormMapSon.put("merge_flag", 2);
//                    //归并确认标识；0-未归并；1-归并待确认；2-归并已确认；3-归并移除
//                    noticeOrderFormMapSon.put("merge_confirm_flag", 1);
//                    //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
//                    noticeOrderFormMapSon.put("material_name", TransUtil.getTransStr(noticeOrderFormMapSon.getStr("material_name")));
//                    noticeOrderMapper.editEntity(noticeOrderFormMapSon);
//                    //新建日志
//                    NoticeOrderExamineLogFormMap noticeOrderExamineLogFormMapSon = new NoticeOrderExamineLogFormMap();
//                    noticeOrderExamineLogFormMapSon.put("notice_order_id", noticeOrderId);
//                    noticeOrderExamineLogFormMap.put("examine_user_code", shiroUser.getLoginName());
//                    noticeOrderExamineLogFormMap.put("examine_user_name", shiroUser.getName());
//                    noticeOrderExamineLogFormMapSon.put("examine_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                    //如果拒绝，插入拒绝说明
//                    if (supAgreeRma == 0) {
//                        //设为拒绝
//                        noticeOrderExamineLogFormMap.put("examine_status", 2);
//                        noticeOrderExamineLogFormMap.put("examine_view", supDisagreeExplain);
//                    } else {
//                        //设为同意
//                        noticeOrderExamineLogFormMap.put("examine_status", 1);
//                        noticeOrderExamineLogFormMap.put("examine_view", "同意。");
//                    }
//                    noticeOrderExamineLogFormMapSon.put("examine_node_name", "供方不良确认");
//                    noticeOrderExamineLogFormMap.put("examine_user_id", shiroUser.getUserId());
//                    noticeOrderExamineLogFormMapSon.put("examine_node_code", 2);
//                    noticeOrderExamineLogMapper.addEntity(noticeOrderExamineLogFormMapSon);
//                    //新建归并关系
//                    NoticeOrderMergeFormMap noticeOrderMergeFormMapSon = new NoticeOrderMergeFormMap();
//                    //归并处理标识 0:初始 1:作为父亲 2:作为儿子
//                    noticeOrderMergeFormMapSon.put("merge_flag", 2);
//                    noticeOrderMergeFormMapSon.put("main_notice_order_id", id);
//                    noticeOrderMergeFormMapSon.put("notice_order_id", noticeOrderId);
//                    noticeOrderMergeFormMapSon.put("supplier_code", shiroUser.getLoginName());
//                    noticeOrderMergeMapper.addEntity(noticeOrderMergeFormMapSon);
//                }
//            }


    }
    //审批
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void examine(QmNoticeOrderDTO param) {
        try {
            final CurrentUserDTO shiroUser = LoginUtil.getCurrentUser();
            final QmNoticeOrder qmNoticeOrder = qmNoticeOrderService.getById(param.getId());
            QmRectifiedMeasures qmRectifiedMeasures = new QmRectifiedMeasures();
            //NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
            param.setId(param.getId());
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUserId(shiroUser.getUserId());
            param.setUpdateUserName(shiroUser.getName());
            param.setSupAgreeTime(LocalDateTime.now());
            //是否同意整改
            param.setSupAgreeRma(param.getSupAgreeRma());
            param.setMergeFlag(0);
            param.setSupAgreeRma(1);

                //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
                param.setStatus(11);
//                param.setIscAcceptDissent(1);
//                param.setIscAcceptDissentTime(LocalDateTime.now());

            //qmRectifiedMeasuresService.save(qmRectifiedMeasures);
            qmNoticeOrderService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity(param));
            qmRectifiedMeasuresService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity1(param));
//            qmRectifiedMeasuresService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity1(param));


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    //审批
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void examine1(QmNoticeOrderDTO param) {
        try {
            final CurrentUserDTO shiroUser = LoginUtil.getCurrentUser();
            final QmNoticeOrder qmNoticeOrder = qmNoticeOrderService.getById(param.getId());
            QmRectifiedMeasures qmRectifiedMeasures = new QmRectifiedMeasures();
            //NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
            param.setId(param.getId());
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUserId(shiroUser.getUserId());
            param.setUpdateUserName(shiroUser.getName());
            param.setSupAgreeTime(LocalDateTime.now());
            //是否同意整改
            param.setSupAgreeRma(param.getSupAgreeRma());
            param.setMergeFlag(0);
            param.setSupAgreeRma(1);

            //状态为待复核;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
            param.setStatus(5);
//                param.setIscAcceptDissent(1);
//                param.setIscAcceptDissentTime(LocalDateTime.now());

            //qmRectifiedMeasuresService.save(qmRectifiedMeasures);
            qmNoticeOrderService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity(param));
//            qmRectifiedMeasuresService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity1(param));


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    /**
     * 供应商整改
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void improve(QmNoticeOrderDTO param) throws DaoException {
        try {
            List<QmNoticeOrderPO> qmNoticeOrderPOS = Lists.newArrayList();
            List<QmNoticeOrderDTO> qmNoticeOrderDTOS = Lists.newArrayList();
            List<QmRectifiedMeasuresDTO> qmRectifiedMeasuresDTOS = JSON.parseArray(param.getRows(), QmRectifiedMeasuresDTO.class);
            Long fatherId = null;
            final CurrentUserDTO shiroUser = LoginUtil.getCurrentUser();
            //父级
//            if (merge_flag == 1) {
//                QmNoticeOrderDTO qmNoticeOrderDTO = new QmNoticeOrderDTO();
//                qmNoticeOrderDTO.put("main_notice_order_id", id);
//                qmNoticeOrderDTOs = noticeOrderMergeMapper.findAllFatherMerge(noticeOrderFormMap);
//                fatherId = id;
//                //子级
//            } else if (merge_flag == 2) {
//                NoticeOrderMergeFormMap noticeOrderMergeFormMap = new NoticeOrderMergeFormMap();
//                noticeOrderMergeFormMap.put("notice_order_id", id);
//                List<NoticeOrderMergeFormMap> noticeOrderMergeFormMaps = noticeOrderMergeMapper.findNoticeOrderMergeList(noticeOrderMergeFormMap);
//                noticeOrderMergeFormMap = noticeOrderMergeFormMaps.get(0);
//                NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
//                noticeOrderFormMap.put("main_notice_order_id", String.valueOf(noticeOrderMergeFormMap.get("main_notice_order_id")));
//                noticeOrderFormMaps = noticeOrderMergeMapper.findAllFatherMerge(noticeOrderFormMap);
//                fatherId = String.valueOf(noticeOrderMergeFormMap.get("main_notice_order_id"));
//            }
           // else {
            QmNoticeOrderDTO qmNoticeOrderDTO = new QmNoticeOrderDTO();
            qmNoticeOrderDTO.setId(param.getId());
            qmNoticeOrderPOS = qmNoticeOrderService.findPageByQueryImprove(param.getId());
            qmNoticeOrderDTOS = qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS);
                fatherId = param.getId();
           // }

            for (QmNoticeOrderDTO a : qmNoticeOrderDTOS) {
                a.setUpdateUserId(shiroUser.getUserId());
                a.setUpdateUserName(shiroUser.getName());
                a.setSupFlowOutReason(a.getSupFlowOutReason());
                a.setSupDeliverReason(a.getSupDeliverReason());
                //根因分类
//                if (!Strings.isNullOrEmpty(rootCause_classify)) {
//                    a.setRootCauseClassify(rootCause_classify);
//                }
// 其他设置值的操作类似
                a.setStatus(12);
                a.setMaterialName(a.getMaterialName());
                if ("QA39".equals(a.getBpmSource())) {
                    a.setFinalBadNumber(a.getFinalBadNumber());
                    a.setFinalBadRate(a.getFinalBadRate());
                    a.setFinalBadDescribe(a.getFinalBadDescribe());
                }

                qmNoticeOrderService.saveOrUpdate(qmNoticeOrderConvert.dto2Entity(a));
               // a.setUpdateTime(LocalDateTime.now());

//                noticeOrderFormMap.put("update_user_id", shiroUser.getUserId());
//                noticeOrderFormMap.put("update_user_name", shiroUser.getName());
//                noticeOrderFormMap.put("sup_flow_out_reason", sup_flow_out_reason);
//                noticeOrderFormMap.put("sup_deliver_reason", sup_deliver_reason);
//                if(!Strings.isNullOrEmpty(rootCause_classify)){
//                    noticeOrderFormMap.put("rootCause_classify", rootCause_classify);
//                }
//                //状态为主管审批不同意;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案 12:已提交改善
//                noticeOrderFormMap.put("status", 12);
//                noticeOrderFormMap.put("material_name", TransUtil.getTransStr(noticeOrderFormMap.getStr("material_name")));
//                if("QA39".equals(noticeOrderFormMap.getStr("bpm_source"))){
//                    noticeOrderFormMap.put("final_bad_number", final_bad_number);
//                    noticeOrderFormMap.put("final_bad_rate", final_bad_rate);
//                    noticeOrderFormMap.put("final_bad_describe", final_bad_describe);
//                }

//                List<String> fjidsList = Lists.newArrayList();
//                fjidsList.addAll(Arrays.asList(StringUtils.split(fjids, ",")));
//                NoticeOrderAttachmentFormMap noticeOrderAttachment = new NoticeOrderAttachmentFormMap();
//                noticeOrderAttachment.put("fjidsList", fjidsList);
//                noticeOrderAttachment.put("relation_id", id);
//                noticeOrderAttachment.put("data_type", 2);
//                noticeOrderAttachmentMapper.deleteNotInIdsByRelationId(noticeOrderAttachment);
//                noticeOrderMapper.editEntity(noticeOrderFormMap);
//                if (StringUtils.isNotEmpty(fjids)) {
//                    for (String fjid : fjids.split(",")) {
//                        NoticeOrderAttachmentFormMap noticeOrderAttachmentFormMap = new NoticeOrderAttachmentFormMap();
//                        noticeOrderAttachmentFormMap.put("relation_id", noticeOrderFormMap.get("id"));
//                        noticeOrderAttachmentFormMap.put("id", fjid);
//                        noticeOrderAttachmentFormMap.put("data_type", 2);
//                        noticeOrderFormMap.put("actual_processing_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                        noticeOrderMapper.editEntity(noticeOrderFormMap);
//                        noticeOrderAttachmentMapper.editEntity(noticeOrderAttachmentFormMap);
//                    }
//                }
            }
            for (QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO : qmRectifiedMeasuresDTOS) {
                //更新
                //if (StringUtils.isNotEmpty(qmRectifiedMeasuresDTO.getId())) {
                    if (Objects.nonNull(qmRectifiedMeasuresDTO.getId())) {
                        qmRectifiedMeasuresDTO.setStatus(0);
                        qmRectifiedMeasuresDTO.setUpdateTime(LocalDateTime.now());
                        qmRectifiedMeasuresDTO.setUpdateUserId(shiroUser.getUserId());
                        qmRectifiedMeasuresDTO.setUpdateUserName(shiroUser.getName());

                        qmRectifiedMeasuresService.updateById(qmRectifiedMeasuresDTO);
                } else {
                        qmRectifiedMeasuresDTO.setNoticeOrderId(fatherId);
                        qmRectifiedMeasuresDTO.setStatus(0);
                        qmRectifiedMeasuresDTO.setCreateTime(LocalDateTime.now());
                        qmRectifiedMeasuresDTO.setCreateUserId(shiroUser.getUserId());
                        qmRectifiedMeasuresDTO.setCreateUserName(shiroUser.getName());
                        qmRectifiedMeasuresService.saveOrUpdate(qmRectifiedMeasuresDTO);
                }

            }
            //qmNoticeOrderService.saveOrUpdate(noticeOrderFormMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * SQE初审
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int examination(QmNoticeOrderDTO param) throws DaoException {
        try {
            List<QmNoticeOrder> noticeOrderFormMaps = Lists.newArrayList();
            String fatherId = null;
            final CurrentUserDTO shiroUser = LoginUtil.getCurrentUser();
//            final Subject subject = SecurityUtils.getSubject();
//            final ShiroUser shiroUser = (ShiroUser) subject.getPrincipal();
            //父级
//            if (merge_flag == 1) {
//                NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
//                noticeOrderFormMap.put("main_notice_order_id", id);
//                noticeOrderFormMaps = noticeOrderMergeMapper.findAllFatherMerge(noticeOrderFormMap);
//                fatherId = id;
//                //子级
//            } else if (merge_flag == 2) {
//                NoticeOrderMergeFormMap noticeOrderMergeFormMap = new NoticeOrderMergeFormMap();
//                noticeOrderMergeFormMap.put("notice_order_id", id);
//                List<NoticeOrderMergeFormMap> noticeOrderMergeFormMaps = noticeOrderMergeMapper.findNoticeOrderMergeList(noticeOrderMergeFormMap);
//                noticeOrderMergeFormMap = noticeOrderMergeFormMaps.get(0);
//                NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
//                noticeOrderFormMap.put("main_notice_order_id", String.valueOf(noticeOrderMergeFormMap.get("main_notice_order_id")));
//                noticeOrderFormMaps = noticeOrderMergeMapper.findAllFatherMerge(noticeOrderFormMap);
//                fatherId = String.valueOf(noticeOrderMergeFormMap.get("main_notice_order_id"));
//            } else {





//                NoticeOrderFormMap noticeOrderFormMap = new NoticeOrderFormMap();
//                noticeOrderFormMap.put("id", id);
//                noticeOrderFormMaps = noticeOrderMapper.findByNoticeOrderPage(noticeOrderFormMap);
//                fatherId = id;
//            //}
//
//
//            for (NoticeOrderFormMap noticeOrderFormMap : noticeOrderFormMaps) {
//
//                noticeOrderFormMap.put("update_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                noticeOrderFormMap.put("update_user_id", shiroUser.getUserId());
//                noticeOrderFormMap.put("update_user_name", shiroUser.getName());
//                //问题等级
//                noticeOrderFormMap.put("sqe_problem_type", sqe_problem_type);
//                noticeOrderFormMap.put("problem_level", problem_level);
//                noticeOrderFormMap.put("restrictive_measures", restrictive_measures);
//                noticeOrderFormMap.put("is_update_qpa", is_update_qpa);
//                noticeOrderFormMap.put("sqe_examine_opinion", sqe_examine_opinion);
//                //初审时间
//                noticeOrderFormMap.put("sqe_examine_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                //是否同意 0:不接受 1:接受
//                noticeOrderFormMap.put("sqe_examine", sqe_examine);
//                //是否推质量案例 0：否，1：是
//                noticeOrderFormMap.put("survey_flag", survey_flag);
//                //同意
//                if (sqe_examine == 1) {
//                    //判断是否推送质量案例,推送先调用接口，成功后再保存
//                    if(survey_flag == 1){
//                        QualitySurveyDto surveyDto = new QualitySurveyDto();
//                        surveyDto.setSurveyTitle(noticeOrderFormMap.getStr("bad_remark"));
//                        surveyDto.setQmCode(noticeOrderFormMap.getStr("notice_order_code"));
//
//                        List<QualitySurveySupplierDto> supplierList = new ArrayList<>();
//                        QualitySurveySupplierDto supplierDto = new QualitySurveySupplierDto();
//                        supplierDto.setSupplierCode(noticeOrderFormMap.getStr("supplier_code"));
//                        supplierDto.setSupplierName(noticeOrderFormMap.getStr("supplier_name"));
//                        supplierList.add(supplierDto);
//
//                        surveyDto.setSupplierList(supplierList);
//
//                        try {
//                            supplierQualitySurveyService.tempSave(surveyDto, shiroUser.getUserId());
//                        }catch (Exception e){
//                            e.printStackTrace();
//                            return 2;
//                        }
//                    }
//                    //状态为待SQE初审;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
//                    noticeOrderFormMap.put("status", 6);
//                    if (StringUtils.isEmpty(sqe_examine_opinion)) {
//                        noticeOrderFormMap.put("sqe_examine_opinion", null);
//                    }
//                } else {
//                    //状态为待SQE初审;0:新建 1:发布 2:供方待确认 3:待复核 4:待SQE初审 5:初审不同意 6:待主管审批 7:主管审批不同意 8:待经理审批 9:经理审批不同意 10:待跟踪 11:已结案
//                    noticeOrderFormMap.put("status", 5);
//                }
//                noticeOrderFormMap.put("material_name", TransUtil.getTransStr(noticeOrderFormMap.getStr("material_name")));
//                noticeOrderMapper.editEntity(noticeOrderFormMap);
//                //新建日志
//                NoticeOrderExamineLogFormMap noticeOrderExamineLogFormMap = new NoticeOrderExamineLogFormMap();
//                //同意
//                if (sqe_examine == 1) {
//                    //日志设为同意
//                    noticeOrderExamineLogFormMap.put("examine_status", 1);
//                    if (StringUtils.isEmpty(sqe_examine_opinion)) {
//                        noticeOrderExamineLogFormMap.put("examine_view", "同意。");
//                    } else {
//                        noticeOrderExamineLogFormMap.put("examine_view", sqe_examine_opinion);
//                    }
//
//                } else {
//                    //日志设为拒绝
//                    noticeOrderExamineLogFormMap.put("examine_status", 0);
//                    noticeOrderExamineLogFormMap.put("examine_view", sqe_examine_opinion);
//                }
//                noticeOrderExamineLogFormMap.put("examine_user_id", shiroUser.getUserId());
//                noticeOrderExamineLogFormMap.put("notice_order_id", noticeOrderFormMap.get("id"));
//                noticeOrderExamineLogFormMap.put("examine_user_code", shiroUser.getLoginName());
//                noticeOrderExamineLogFormMap.put("examine_user_name", shiroUser.getName());
//                noticeOrderExamineLogFormMap.put("examine_time", com.jtech.toa.project.common.DateUtil.currentDateTimeToString());
//                noticeOrderExamineLogFormMap.put("examine_node_name", "SQE初审");
//                noticeOrderExamineLogFormMap.put("examine_node_code", 4);
//                noticeOrderExamineLogMapper.addEntity(noticeOrderExamineLogFormMap);
//            }

            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    /**
     * 删除整改措施
     *
     * @param ids 整改措施ids
     */
    @Override
    public void removeMeasures(String ids) throws DaoException {
        try {
            for (String id : ids.split(",")) {
                qmRectifiedMeasuresService.removeById(id);
            }
               // qmRectifiedMeasuresService.removeById(id);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 跟踪状态
     *
     */
    @Override
    public void followingChange(Long id, Integer followingStatus) {
        QmNoticeOrder qmNoticeOrder = new QmNoticeOrder();
        qmNoticeOrder.setId(id);
        qmNoticeOrder.setFollowingStatus(followingStatus);
        qmNoticeOrderService.updateById(qmNoticeOrder);
    }
    /**
     * 发布
     *
     * @param id 质量整改ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void release(Long id) {
        try {
            final CurrentUserDTO user =  LoginUtil.getCurrentUser();
            QmNoticeOrder qmNoticeOrder = new QmNoticeOrder();
            qmNoticeOrder.setId(id);
            qmNoticeOrder.setCreateTime(LocalDateTime.now());
            qmNoticeOrder.setUpdateTime(LocalDateTime.now());
            qmNoticeOrder.setReleaseTime(LocalDateTime.now());
            qmNoticeOrder.setUpdateUserId(user.getUserId());
            qmNoticeOrder.setUpdateUserName(user.getName());
            //状态为供方待确认
            qmNoticeOrder.setStatus(4);
            //noticeOrderFormMap.put("material_name", TransUtil.getTransStr(noticeOrderFormMap.getStr("material_name")));
            qmNoticeOrderService.updateById(qmNoticeOrder);
            QmNoticeOrderExamineLog qmNoticeOrderExamineLog = new QmNoticeOrderExamineLog();
            qmNoticeOrderExamineLog.setId(id);
            qmNoticeOrderExamineLog.setExamineUserCode(user.getLoginName());
            qmNoticeOrderExamineLog.setExamineUserName(user.getName());
            qmNoticeOrderExamineLog.setExamineUserId(user.getUserId());
            qmNoticeOrderExamineLog.setExamineStatus(1);
            qmNoticeOrderExamineLog.setExamineTime(LocalDateTime.now());
            qmNoticeOrderExamineLog.setExamineView("同意。");
            qmNoticeOrderExamineLog.setExamineNodeName("发布");
            qmNoticeOrderExamineLog.setExamineNodeCode("1");
            qmNoticeOrderExamineLogService.saveOrUpdate(qmNoticeOrderExamineLog);
            List<String> quoteSupCodeList = new ArrayList<>();
            quoteSupCodeList.add(qmNoticeOrder.getSupplierCode());
            //发送质量整改邮件
//            if (isSend) {
//                NoticeOrderFormMap noticeOrderFormMap1 = new NoticeOrderFormMap();
//                noticeOrderFormMap1.put("id", id);
//                this.sendMail(noticeOrderMapper.findNoticeOrder(noticeOrderFormMap1), noticeOrderFormMap.getStr("supplier_code"), shiroUser.getUserId(), 0);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    //根据id查询数据
    @Override
    public QmNoticeOrderDTO getById(Long id) {
        if (id != null) {
            QmNoticeOrderPO qmNoticeOrderPO = qmNoticeOrderService.getDataById(id);
            return qmNoticeOrderConvert.po2DTO(qmNoticeOrderPO);
        }else{
            return new QmNoticeOrderDTO();
        }
    }

    //删除
    @Override
    public void remove(String noticeOrderCode) {
        //删除
        qmNoticeOrderService.remove(new LambdaQueryWrapper<QmNoticeOrder>().eq(QmNoticeOrder::getNoticeOrderCode, noticeOrderCode));
    }
}
