package com.hw.qms.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.hw.common.core.constant.QmsConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.qms.domain.*;
import com.hw.qms.domain.vo.QmsCheckResultDetailVo;
import com.hw.qms.domain.vo.QmsMaterialCheckVo;
import com.hw.qms.domain.vo.QmsQualityInstanceActivityVo;
import com.hw.qms.mapper.QmsCheckResultDetailProjectMapper;
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.mapper.QmsCheckResultDetailMapper;
import com.hw.qms.service.IQmsCheckResultDetailService;

/**
 * 质检结果详情Service业务层处理
 *
 * @author xins
 * @date 2024-01-23
 */
@Service
public class QmsCheckResultDetailServiceImpl implements IQmsCheckResultDetailService {
    @Autowired
    private QmsCheckResultDetailMapper qmsCheckResultDetailMapper;

    @Autowired
    private QmsCheckResultDetailProjectMapper qmsCheckResultDetailProjectMapper;

    /**
     * 查询质检结果详情
     *
     * @param checkResultDetailId 质检结果详情主键
     * @return 质检结果详情
     */
    @Override
    public QmsCheckResultDetail selectQmsCheckResultDetailByCheckResultDetailId(Long checkResultDetailId) {
        return qmsCheckResultDetailMapper.selectQmsCheckResultDetailByCheckResultDetailId(checkResultDetailId);
    }

    /**
     * 查询质检结果详情列表
     *
     * @param qmsCheckResultDetail 质检结果详情
     * @return 质检结果详情
     */
    @Override
    public List<QmsCheckResultDetail> selectQmsCheckResultDetailList(QmsCheckResultDetail qmsCheckResultDetail) {
        List<QmsCheckResultDetail> qmsCheckResultDetails = qmsCheckResultDetailMapper.selectQmsCheckResultDetailList(qmsCheckResultDetail);

        for (QmsCheckResultDetail q:qmsCheckResultDetails){
            QmsCheckResultDetailProject qmsCheckResultDetailProject = new QmsCheckResultDetailProject();
            qmsCheckResultDetailProject.setCheckResultDetailId(q.getCheckResultDetailId());
            List<QmsCheckResultDetailProject> qmsCheckResultDetailProjects = qmsCheckResultDetailProjectMapper.selectQmsCheckResultDetailProjectList(qmsCheckResultDetailProject);
            q.setQmsCheckResultDetailProjectList(qmsCheckResultDetailProjects);
        }
        return qmsCheckResultDetails;



    }

    /**
     * 新增质检结果详情
     *
     * @param qmsCheckResultDetail 质检结果详情
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQmsCheckResultDetail(QmsCheckResultDetail qmsCheckResultDetail) {
        Date currentDate = new Date();
        String userName = SecurityUtils.getUsername();
        qmsCheckResultDetail.setCreateTime(currentDate);
        qmsCheckResultDetail.setCreateBy(userName);
        QmsCheckResultDetail returnCheckResultDetail = handleQmsCheckResultDetailProject(qmsCheckResultDetail);
        if (returnCheckResultDetail != null) {
            qmsCheckResultDetail.setCheckStatus(returnCheckResultDetail.getCheckStatus());
            int rows = qmsCheckResultDetailMapper.insertQmsCheckResultDetail(qmsCheckResultDetail);
            qmsCheckResultDetail.setQmsCheckResultDetailProjectList(returnCheckResultDetail.getQmsCheckResultDetailProjectList());
            batchQmsCheckResultDetailProject(qmsCheckResultDetail);
            return rows;
        }
        return 0;
    }

    /**
     * 修改质检结果详情
     *
     * @param qmsCheckResultDetail 质检结果详情
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQmsCheckResultDetail(QmsCheckResultDetail qmsCheckResultDetail) {
        String userName = SecurityUtils.getUsername();
        Date currentDate = new Date();
        qmsCheckResultDetail.setUpdateTime(currentDate);
        qmsCheckResultDetail.setUpdateBy(userName);

        QmsCheckResultDetail returnCheckResultDetail = handleQmsCheckResultDetailProject(qmsCheckResultDetail);
        if (returnCheckResultDetail != null) {
            qmsCheckResultDetail.setCheckStatus(returnCheckResultDetail.getCheckStatus());
            qmsCheckResultDetail.setQmsCheckResultDetailProjectList(returnCheckResultDetail.getQmsCheckResultDetailProjectList());
            batchUpdateQmsCheckResultDetailProject(qmsCheckResultDetail);
        }

        return qmsCheckResultDetailMapper.updateQmsCheckResultDetail(qmsCheckResultDetail);
    }

    /**
     * 批量删除质检结果详情
     *
     * @param checkResultDetailIds 需要删除的质检结果详情主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsCheckResultDetailByCheckResultDetailIds(Long[] checkResultDetailIds) {
        qmsCheckResultDetailMapper.deleteQmsCheckResultDetailProjectByCheckResultDetailIds(checkResultDetailIds);
        return qmsCheckResultDetailMapper.deleteQmsCheckResultDetailByCheckResultDetailIds(checkResultDetailIds);
    }

    /**
     * 删除质检结果详情信息
     *
     * @param checkResultDetailId 质检结果详情主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsCheckResultDetailByCheckResultDetailId(Long checkResultDetailId) {
        qmsCheckResultDetailMapper.deleteQmsCheckResultDetailProjectByCheckResultDetailId(checkResultDetailId);
        return qmsCheckResultDetailMapper.deleteQmsCheckResultDetailByCheckResultDetailId(checkResultDetailId);
    }

    /**
     * 新增质检结果详情项目信息信息
     *
     * @param qmsCheckResultDetail 质检结果详情对象
     */
    public QmsCheckResultDetail handleQmsCheckResultDetailProject(QmsCheckResultDetail qmsCheckResultDetail) {
        String checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_PASS;//质检结果检验状态
        List<QmsCheckResultDetailProject> qmsCheckResultDetailProjectList = qmsCheckResultDetail.getQmsCheckResultDetailProjectList();
        if (StringUtils.isNotNull(qmsCheckResultDetailProjectList)) {
            QmsCheckResultDetail returnCheckResultDetail = new QmsCheckResultDetail();
            List<QmsCheckResultDetailProject> list = new ArrayList<QmsCheckResultDetailProject>();
            for (QmsCheckResultDetailProject qmsCheckResultDetailProject : qmsCheckResultDetailProjectList) {

                String checkProjectProperty = qmsCheckResultDetailProject.getCheckProjectProperty();
                if (checkProjectProperty.equals(QmsConstants.QMS_CHECK_PROJECT_PROPERTY_QUALITATIVE)) {//如果是定性
                    String checkProjectStatus = qmsCheckResultDetailProject.getCheckProjectStatus();
                    if (checkProjectStatus != null) {
                        if (checkProjectStatus.equals(QmsConstants.QMS_CHECK_PROJECT_STATUS_FAIL)) {
                            checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_FAIL;
                        }
                    } else {
                        if (checkStatus.equals(QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_PASS)) {
                            checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_CHECKED;
                        }
                    }
                } else {//如果是定量
                    BigDecimal checkProjectResult = qmsCheckResultDetailProject.getCheckProjectResult();
                    if (checkProjectResult != null) {
                        BigDecimal standardValue = qmsCheckResultDetailProject.getStandardValue();
                        BigDecimal upperDiff = qmsCheckResultDetailProject.getUpperDiff();
                        BigDecimal downDiff = qmsCheckResultDetailProject.getDownDiff();
                        if (upperDiff == null && downDiff == null) {
                            if (checkProjectResult.compareTo(standardValue) == 0) {
                                qmsCheckResultDetailProject.setCheckProjectStatus(QmsConstants.QMS_CHECK_PROJECT_STATUS_PASS);
                            } else {
                                qmsCheckResultDetailProject.setCheckProjectStatus(QmsConstants.QMS_CHECK_PROJECT_STATUS_FAIL);
                                checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_FAIL;
                            }
                        } else {
                            if (upperDiff == null) {//如果没设置上差值，则标准值为上差值
                                upperDiff = standardValue;
                            } else if (downDiff == null) {//如果没设置下差值，则标准值为下差值
                                downDiff = standardValue;
                            }
                            if (checkProjectResult.compareTo(downDiff) >= 0 && checkProjectResult.compareTo(upperDiff) <= 0) {
                                qmsCheckResultDetailProject.setCheckProjectStatus(QmsConstants.QMS_CHECK_PROJECT_STATUS_PASS);
                            } else {
                                qmsCheckResultDetailProject.setCheckProjectStatus(QmsConstants.QMS_CHECK_PROJECT_STATUS_FAIL);
                                checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_FAIL;
                            }
                        }
                    } else {
                        if (checkStatus.equals(QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_PASS)) {
                            checkStatus = QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_CHECKED;
                        }
                    }
                }

                list.add(qmsCheckResultDetailProject);
            }

            returnCheckResultDetail.setCheckStatus(checkStatus);
            returnCheckResultDetail.setQmsCheckResultDetailProjectList(list);

            return returnCheckResultDetail;
        }


        return null;
    }


    /**
     * 批量插入质检结果详情项目信息(事务，需要用public，不能用private)
     */
    public void batchQmsCheckResultDetailProject(QmsCheckResultDetail qmsCheckResultDetail) {
        List<QmsCheckResultDetailProject> qmsCheckResultDetailProjectList = qmsCheckResultDetail.getQmsCheckResultDetailProjectList();
        Long checkResultDetailId = qmsCheckResultDetail.getCheckResultDetailId();
        if (StringUtils.isNotNull(checkResultDetailId)) {
            List<QmsCheckResultDetailProject> list = new ArrayList<QmsCheckResultDetailProject>();
            for (QmsCheckResultDetailProject qmsCheckResultDetailProject : qmsCheckResultDetailProjectList) {
                qmsCheckResultDetailProject.setCheckResultDetailId(checkResultDetailId);
                list.add(qmsCheckResultDetailProject);
            }
            if (list.size() > 0) {
                qmsCheckResultDetailMapper.batchQmsCheckResultDetailProject(list);
            }
        }
    }

    /**
     * 批量更新质检结果详情项目信息(事务，需要用public，不能用private)
     */
    public void batchUpdateQmsCheckResultDetailProject(QmsCheckResultDetail qmsCheckResultDetail) {
        List<QmsCheckResultDetailProject> qmsCheckResultDetailProjectList = qmsCheckResultDetail.getQmsCheckResultDetailProjectList();
        for (QmsCheckResultDetailProject qmsCheckResultDetailProject : qmsCheckResultDetailProjectList) {
            qmsCheckResultDetailProjectMapper.updateQmsCheckResultDetailProject(qmsCheckResultDetailProject);
        }

    }


    /**
     * 获取质检结果详情
     *
     * @param materialBarcode 物料条码
     * @return
     */
    @Override
    public QmsCheckResultDetail getCheckResultDetail(String materialBarcode) {
        //现获取质检结果with质检项目结果详情信息
        QmsCheckResultDetail checkResultDetail = qmsCheckResultDetailMapper.selectQmsCheckResultDetailByMaterialBarcode(materialBarcode);
        return checkResultDetail;
    }


    /**
     * 查询质检结果批次审核结果数量
     *
     * @param checkResultId 质检结果主键
     * @return
     */
    @Override
    public QmsCheckResultDetailVo getCheckResultDetailVo(Long checkResultId) {
        QmsCheckResultDetail passResultDetail = new QmsCheckResultDetail();
        passResultDetail.setCheckResultId(checkResultId);
        passResultDetail.setCheckStatus(QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_PASS);
        int passAmount = qmsCheckResultDetailMapper.selectResultDetailQualityCountByResultDetailId(passResultDetail);

        QmsCheckResultDetail failResultDetail = new QmsCheckResultDetail();
        failResultDetail.setCheckResultId(checkResultId);
        failResultDetail.setCheckStatus(QmsConstants.QMS_CHECK_RESULT_CHECK_STATUS_FAIL);
        int failAmount = qmsCheckResultDetailMapper.selectResultDetailQualityCountByResultDetailId(failResultDetail);

        QmsCheckResultDetailVo checkResultDetailVo = new QmsCheckResultDetailVo();
        checkResultDetailVo.setPassAmount(passAmount);
        checkResultDetailVo.setFailAmount(failAmount);
        checkResultDetailVo.setCheckResultId(checkResultId);
        return checkResultDetailVo;
    }


}
