package com.hw.qms.service.impl;

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

import com.hw.common.core.constant.QmsConstants;
import com.hw.common.core.constant.WmsConstants;
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.QmsMaterialCheckVo;
import com.hw.qms.mapper.*;
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.service.IQmsCheckRuleService;

/**
 * 检验规则维护Service业务层处理
 *
 * @author xins
 * @date 2024-01-25
 */
@Service
public class QmsCheckRuleServiceImpl implements IQmsCheckRuleService {
    @Autowired
    private QmsCheckRuleMapper qmsCheckRuleMapper;

    @Autowired
    private QmsCheckResultDetailMapper qmsCheckResultDetailMapper;


    /**
     * 查询检验规则维护
     *
     * @param checkRuleId 检验规则维护主键
     * @return 检验规则维护
     */
    @Override
    public QmsCheckRule selectQmsCheckRuleByCheckRuleId(Long checkRuleId) {
        return qmsCheckRuleMapper.selectQmsCheckRuleByCheckRuleId(checkRuleId);
    }

    /**
     * 查询检验规则维护列表
     *
     * @param qmsCheckRule 检验规则维护
     * @return 检验规则维护
     */
    @Override
    public List<QmsCheckRule> selectQmsCheckRuleList(QmsCheckRule qmsCheckRule) {
        return qmsCheckRuleMapper.selectQmsCheckRuleList(qmsCheckRule);
    }

    /**
     * 新增检验规则维护
     *
     * @param qmsCheckRule 检验规则维护
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQmsCheckRule(QmsCheckRule qmsCheckRule) {
        qmsCheckRule.setCreateBy(SecurityUtils.getUsername());
        qmsCheckRule.setCreateTime(DateUtils.getNowDate());
        if (qmsCheckRule.getCheckMode().equals(QmsConstants.QMS_CHECK_MODE)) {
            qmsCheckRule.setCheckSample(null);
        }

        int rows = qmsCheckRuleMapper.insertQmsCheckRule(qmsCheckRule);
        insertQmsCheckRuleProject(qmsCheckRule);
        return rows;
    }

    /**
     * 修改检验规则维护
     *
     * @param qmsCheckRule 检验规则维护
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQmsCheckRule(QmsCheckRule qmsCheckRule) {
        qmsCheckRule.setUpdateBy(SecurityUtils.getUsername());
        qmsCheckRule.setUpdateTime(DateUtils.getNowDate());
        if (qmsCheckRule.getCheckMode().equals(QmsConstants.QMS_CHECK_MODE)) {
            qmsCheckRule.setCheckSample(null);
        }
        qmsCheckRuleMapper.deleteQmsCheckRuleProjectByCheckRuleId(qmsCheckRule.getCheckRuleId());
        insertQmsCheckRuleProject(qmsCheckRule);
        return qmsCheckRuleMapper.updateQmsCheckRule(qmsCheckRule);
    }

    /**
     * 批量删除检验规则维护
     *
     * @param checkRuleIds 需要删除的检验规则维护主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsCheckRuleByCheckRuleIds(Long[] checkRuleIds) {
        qmsCheckRuleMapper.deleteQmsCheckRuleProjectByCheckRuleIds(checkRuleIds);
        return qmsCheckRuleMapper.deleteQmsCheckRuleByCheckRuleIds(checkRuleIds);
    }

    /**
     * 删除检验规则维护信息
     *
     * @param checkRuleId 检验规则维护主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQmsCheckRuleByCheckRuleId(Long checkRuleId) {
        qmsCheckRuleMapper.deleteQmsCheckRuleProjectByCheckRuleId(checkRuleId);
        return qmsCheckRuleMapper.deleteQmsCheckRuleByCheckRuleId(checkRuleId);
    }


    /**
     * 新增检验规则项目信息
     *
     * @param qmsCheckRule 检验规则维护对象
     */
    public void insertQmsCheckRuleProject(QmsCheckRule qmsCheckRule)
    {
        List<QmsCheckRuleProject> qmsCheckRuleProjectList = qmsCheckRule.getQmsCheckRuleProjectList();
        Long checkRuleId = qmsCheckRule.getCheckRuleId();
        if (StringUtils.isNotNull(qmsCheckRuleProjectList))
        {
            List<QmsCheckRuleProject> list = new ArrayList<QmsCheckRuleProject>();
            for (QmsCheckRuleProject qmsCheckRuleProject : qmsCheckRuleProjectList)
            {
                qmsCheckRuleProject.setCheckRuleId(checkRuleId);
                list.add(qmsCheckRuleProject);
            }
            if (list.size() > 0)
            {
                qmsCheckRuleMapper.batchQmsCheckRuleProject(list);
            }
        }
    }


    /**
     * 批量插入检验规则明细
     * @param checkRuleDetailList
     */
    @Override
    public int batchInsertCheckRuleDetail(List<QmsCheckRuleDetail> checkRuleDetailList) {
        return qmsCheckRuleMapper.batchQmsCheckRuleDetail(checkRuleDetailList);
    }


    /**
     * 批量插入检验规则项目
     * @param checkRuleProjectList
     */
    @Override
    public int batchInsertCheckRuleProject(List<QmsCheckRuleProject> checkRuleProjectList) {
        return qmsCheckRuleMapper.batchQmsCheckRuleProject(checkRuleProjectList);
    }


    /**
     * 查询检验规则维护，连接check_rule_project
     *
     * @param qmsCheckRuleDetail 检验规则明细
     * @return 检验规则维护
     */
    @Override
    public QmsCheckRule selectQmsCheckRuleByTarget(QmsCheckRuleDetail qmsCheckRuleDetail) {
        return qmsCheckRuleMapper.selectQmsCheckRuleByTarget(qmsCheckRuleDetail);
    }


}
