package com.hw.qms.service.impl;

import java.util.Date;
import java.util.List;

import com.hw.common.core.constant.WfConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.qms.domain.QmsCheckInstanceFile;
import com.hw.qms.domain.vo.QmsQualityInstanceActivityVo;
import com.hw.qms.mapper.QmsQualityInstanceActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.qms.domain.QmsQualityInstanceActivity;
import com.hw.qms.mapper.QmsQualityInstanceMapper;
import com.hw.qms.domain.QmsQualityInstance;
import com.hw.qms.service.IQmsQualityInstanceService;

/**
 * 质检工单Service业务层处理
 *
 * @author xins
 * @date 2024-01-23
 */
@Service
public class QmsQualityInstanceServiceImpl implements IQmsQualityInstanceService {
    @Autowired
    private QmsQualityInstanceMapper qmsQualityInstanceMapper;

    @Autowired
    private QmsQualityInstanceActivityMapper qmsQualityInstanceActivityMapper;

    /**
     * 查询质检工单
     *
     * @param qualityInstanceId 质检工单主键
     * @return 质检工单
     */
    @Override
    public QmsQualityInstance selectQmsQualityInstanceByQualityInstanceId(Long qualityInstanceId) {
        return qmsQualityInstanceMapper.selectQmsQualityInstanceByQualityInstanceId(qualityInstanceId);
    }

    /**
     * 查询质检工单列表
     *
     * @param qmsQualityInstance 质检工单
     * @return 质检工单
     */
    @Override
    public List<QmsQualityInstance> selectQmsQualityInstanceList(QmsQualityInstance qmsQualityInstance) {
        return qmsQualityInstanceMapper.selectQmsQualityInstanceList(qmsQualityInstance);
    }

    /**
     * 新增质检工单
     *
     * @param qmsQualityInstance 质检工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQmsQualityInstance(QmsQualityInstance qmsQualityInstance) {
        qmsQualityInstance.setCreateTime(DateUtils.getNowDate());
        int rows = qmsQualityInstanceMapper.insertQmsQualityInstance(qmsQualityInstance);
        insertQmsQualityInstanceActivity(qmsQualityInstance);
        return rows;
    }

    /**
     * 修改质检工单
     *
     * @param qmsQualityInstance 质检工单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQmsQualityInstance(QmsQualityInstance qmsQualityInstance) {
        qmsQualityInstance.setUpdateTime(DateUtils.getNowDate());
        qmsQualityInstanceMapper.deleteQmsQualityInstanceActivityByQualityInstanceId(qmsQualityInstance.getQualityInstanceId());
        insertQmsQualityInstanceActivity(qmsQualityInstance);
        return qmsQualityInstanceMapper.updateQmsQualityInstance(qmsQualityInstance);
    }

    /**
     * 批量删除质检工单
     *
     * @param qualityInstanceIds 需要删除的质检工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsQualityInstanceByQualityInstanceIds(Long[] qualityInstanceIds) {
        qmsQualityInstanceMapper.deleteQmsQualityInstanceActivityByQualityInstanceIds(qualityInstanceIds);
        return qmsQualityInstanceMapper.deleteQmsQualityInstanceByQualityInstanceIds(qualityInstanceIds);
    }

    /**
     * 删除质检工单信息
     *
     * @param qualityInstanceId 质检工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsQualityInstanceByQualityInstanceId(Long qualityInstanceId) {
        qmsQualityInstanceMapper.deleteQmsQualityInstanceActivityByQualityInstanceId(qualityInstanceId);
        return qmsQualityInstanceMapper.deleteQmsQualityInstanceByQualityInstanceId(qualityInstanceId);
    }

    /**
     * 新增质检工单实例节点信息
     *
     * @param qmsQualityInstance 质检工单对象
     */
    public void insertQmsQualityInstanceActivity(QmsQualityInstance qmsQualityInstance) {
        List<QmsQualityInstanceActivity> qmsQualityInstanceActivityList = qmsQualityInstance.getQmsQualityInstanceActivityList();
        Long qualityInstanceId = qmsQualityInstance.getQualityInstanceId();
        if (StringUtils.isNotNull(qmsQualityInstanceActivityList)) {
            List<QmsQualityInstanceActivity> list = new ArrayList<QmsQualityInstanceActivity>();
            for (QmsQualityInstanceActivity qmsQualityInstanceActivity : qmsQualityInstanceActivityList) {
                qmsQualityInstanceActivity.setQualityInstanceId(qualityInstanceId);
                list.add(qmsQualityInstanceActivity);
            }
            if (list.size() > 0) {
                qmsQualityInstanceMapper.batchQmsQualityInstanceActivity(list);
            }
        }
    }


    /**
     * 查询质检工单
     *
     * @param checkResultId 质检结果ID
     * @return 质检工单
     */
    @Override
    public QmsQualityInstance selectQmsQualityInstanceByCheckResultId(Long checkResultId) {
        return qmsQualityInstanceMapper.selectQmsQualityInstanceByCheckResultId(checkResultId);
    }


    /**
     * 不合格处置保存
     *
     * @param qmsQualityInstanceActivityVo
     * @param fileUrls
     */
    @Transactional
    @Override
    public int handleQualityInstance(QmsQualityInstanceActivityVo qmsQualityInstanceActivityVo, List<String> fileUrls) {
        Long checkResultId = qmsQualityInstanceActivityVo.getCheckResultId();
        QmsQualityInstance qmsQualityInstance = qmsQualityInstanceMapper.selectQmsQualityInstanceByCheckResultId(checkResultId);
        Date currentDate = new Date();
        String userName = SecurityUtils.getUsername();
        //todo:权限校验
        if (qmsQualityInstance == null) {
            qmsQualityInstance = new QmsQualityInstance();
            qmsQualityInstance.setCheckResultId(checkResultId);
            qmsQualityInstance.setWfProcessId(WfConstants.WF_QUALITY_ABNORMAL_PROCESS_ID);
            qmsQualityInstance.setWfInstanceStatus(WfConstants.WF_INSTANCE_STATUS_START);
            qmsQualityInstance.setStartTime(currentDate);
            qmsQualityInstance.setCreateTime(currentDate);
            qmsQualityInstance.setCreateBy(userName);

            int rows = qmsQualityInstanceMapper.insertQmsQualityInstance(qmsQualityInstance);

            QmsQualityInstanceActivity qmsQualityInstanceActivity = new QmsQualityInstanceActivity();
            qmsQualityInstanceActivity.setQualityInstanceId(qmsQualityInstance.getQualityInstanceId());
            qmsQualityInstanceActivity.setProcessActivityId(qmsQualityInstanceActivityVo.getProcessActivityId());
            qmsQualityInstanceActivity.setProcessHandleResolution(qmsQualityInstanceActivityVo.getProcessHandleResolution());
            qmsQualityInstanceActivity.setProcessStepOrder(qmsQualityInstanceActivityVo.getProcessStepOrder());
            qmsQualityInstanceActivity.setProcessHandleStatus(WfConstants.WF_PROCESS_HANDLE_STATUS_FINISH);
            qmsQualityInstanceActivity.setStartTime(currentDate);
            qmsQualityInstanceActivity.setEndTime(currentDate);
            qmsQualityInstanceActivity.setHandleUserId(SecurityUtils.getUserId());
            qmsQualityInstanceActivity.setHandleBy(userName);
            qmsQualityInstanceActivity.setHandleTime(currentDate);
            qmsQualityInstanceActivity.setCreateBy(userName);
            qmsQualityInstanceActivity.setCreateTime(currentDate);

            qmsQualityInstanceActivityMapper.insertQmsQualityInstanceActivity(qmsQualityInstanceActivity);
            insertQmsCheckInstanceFile(qmsQualityInstanceActivity, fileUrls);

            return rows;

        } else {
            QmsQualityInstanceActivity qmsQualityInstanceActivity = new QmsQualityInstanceActivity();
            qmsQualityInstanceActivity.setQualityInstanceId(qmsQualityInstance.getQualityInstanceId());
            qmsQualityInstanceActivity.setProcessActivityId(qmsQualityInstanceActivityVo.getProcessActivityId());
            qmsQualityInstanceActivity.setProcessHandleResolution(qmsQualityInstanceActivityVo.getProcessHandleResolution());
            qmsQualityInstanceActivity.setProcessStepOrder(qmsQualityInstanceActivityVo.getProcessStepOrder());
            qmsQualityInstanceActivity.setProcessHandleStatus(WfConstants.WF_PROCESS_HANDLE_STATUS_FINISH);
            qmsQualityInstanceActivity.setStartTime(currentDate);
            qmsQualityInstanceActivity.setEndTime(currentDate);
            qmsQualityInstanceActivity.setHandleUserId(SecurityUtils.getUserId());
            qmsQualityInstanceActivity.setHandleBy(userName);
            qmsQualityInstanceActivity.setHandleTime(currentDate);
            qmsQualityInstanceActivity.setCreateBy(userName);
            qmsQualityInstanceActivity.setCreateTime(currentDate);

            //todo:如果工单流程增加到2步以上，需要判断是否是最后一步再处理
            qmsQualityInstance.setEndTime(currentDate);
            qmsQualityInstance.setWfInstanceStatus(WfConstants.WF_INSTANCE_STATUS_FINISH);
            qmsQualityInstance.setUpdateTime(currentDate);
            qmsQualityInstance.setUpdateBy(userName);
            qmsQualityInstanceMapper.updateQmsQualityInstance(qmsQualityInstance);

            return qmsQualityInstanceActivityMapper.insertQmsQualityInstanceActivity(qmsQualityInstanceActivity);
        }
    }


    /**
     * 新增质检文件信息
     *
     * @param qmsQualityInstanceActivity 质检工单实例节点对象
     * @param fileUrls                   文件url列表
     */
    public void insertQmsCheckInstanceFile(QmsQualityInstanceActivity qmsQualityInstanceActivity, List<String> fileUrls) {
        Long instanceActivityId = qmsQualityInstanceActivity.getInstanceActivityId();
        if (StringUtils.isNotNull(fileUrls)) {
            List<QmsCheckInstanceFile> list = new ArrayList<QmsCheckInstanceFile>();
            for (String fileUrl : fileUrls) {
                QmsCheckInstanceFile qmsCheckInstanceFile = new QmsCheckInstanceFile();
                qmsCheckInstanceFile.setTargetId(instanceActivityId);
                qmsCheckInstanceFile.setTargetType(WfConstants.QMS_FILE_TARGET_TYPE_QUALITY_INSTANCE_ACTIVITY);
                qmsCheckInstanceFile.setFaultFile(fileUrl);
                list.add(qmsCheckInstanceFile);
            }
            if (list.size() > 0) {
                qmsQualityInstanceActivityMapper.batchQmsCheckInstanceFile(list);
            }
        }
    }

}
