package com.aucma.report.service.impl;

import java.util.HashMap;
import java.util.List;

import com.aucma.base.domain.BaseProductLine;
import com.aucma.base.service.IBaseProductLineService;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.ReportQualityInspectionMapper;
import com.aucma.report.domain.ReportQualityInspection;
import com.aucma.report.service.IReportQualityInspectionService;

/**
 * 质检记录管理Service业务层处理
 *
 * @author Yinq
 * @date 2023-11-21
 */
@Service
public class ReportQualityInspectionServiceImpl implements IReportQualityInspectionService {
    @Autowired
    private ReportQualityInspectionMapper reportQualityInspectionMapper;

    @Autowired
    private IBaseProductLineService productLineService;

    /**
     * 查询质检记录管理
     *
     * @param objId 质检记录管理主键
     * @return 质检记录管理
     */
    @Override
    public ReportQualityInspection selectReportQualityInspectionByObjId(Long objId) {
        return reportQualityInspectionMapper.selectReportQualityInspectionByObjId(objId);
    }

    /**
     * 查询质检记录管理列表
     *
     * @param reportQualityInspection 质检记录管理
     * @return 质检记录管理
     */
    @Override
    public List<ReportQualityInspection> selectReportQualityInspectionList(ReportQualityInspection reportQualityInspection) {
        return reportQualityInspectionMapper.selectReportQualityInspectionList(reportQualityInspection);
    }

    /**
     * 新增质检记录管理
     *
     * @param reportQualityInspection 质检记录管理
     * @return 结果
     */
    @Override
    public int insertReportQualityInspection(ReportQualityInspection reportQualityInspection) {
        return reportQualityInspectionMapper.insertReportQualityInspection(reportQualityInspection);
    }

    /**
     * 修改质检记录管理
     *
     * @param reportQualityInspection 质检记录管理
     * @return 结果
     */
    @Override
    public int updateReportQualityInspection(ReportQualityInspection reportQualityInspection) {
        return reportQualityInspectionMapper.updateReportQualityInspection(reportQualityInspection);
    }

    /**
     * 批量删除质检记录管理
     *
     * @param objIds 需要删除的质检记录管理主键
     * @return 结果
     */
    @Override
    public int deleteReportQualityInspectionByObjIds(Long[] objIds) {
        return reportQualityInspectionMapper.deleteReportQualityInspectionByObjIds(objIds);
    }

    /**
     * 删除质检记录管理信息
     *
     * @param objId 质检记录管理主键
     * @return 结果
     */
    @Override
    public int deleteReportQualityInspectionByObjId(Long objId) {
        return reportQualityInspectionMapper.deleteReportQualityInspectionByObjId(objId);
    }

    /**
     * PDA查询当班质检数量
     *
     * @param stationCode
     * @return
     */
    @Override
    public HashMap<String, Integer> pdaQueryQuantityNumber(String stationCode) {
        if (StringUtils.isEmpty(stationCode)){
            return new HashMap<>();
        }
        return reportQualityInspectionMapper.pdaQueryQuantityNumber(stationCode);
    }




    /**
     * 箱体码查电检数据
     * @param barcode
     * @param productLineCode
     * @return
     */
    @Override
    public ReportQualityInspection selectReportQualityInspectionByBarcode(String barcode, String productLineCode) {
        if (productLineCode.equals("CX_02")){
            return reportQualityInspectionMapper.selectReportQualityInspectionByBarcode(barcode);
        } else {
            return reportQualityInspectionMapper.selectOneReportQualityInspectionByBarcode(barcode);
        }
    }

    /**
     * 检验前一工位是否已完成质检
     * @param boxCode
     * @param stationCode
     * @return
     */
    @Override
    public String checkBeforeStationInspection(String boxCode, String stationCode) {
        String result = null;
        BaseProductLine baseProductLine = new BaseProductLine();
        baseProductLine.setProductLineCode(stationCode);
        List<BaseProductLine> productLineList = productLineService.selectReportQualityStationCode(baseProductLine);
        int markLocation = 0;
        for (int i = 0; i < productLineList.size(); i++) {
            if (productLineList.get(i).getProductLineCode().equals(stationCode)){
                markLocation = i;
            }
        }
        if (markLocation <= 1){
            return null;
        }
        BaseProductLine productLineInfo = productLineList.get(markLocation - 1);
        ReportQualityInspection inspection = new ReportQualityInspection();
        inspection.setBarCode(boxCode);
        inspection.setStationCode(productLineInfo.getProductLineCode());
        inspection.setTreatmentMeasure("3");
        List<ReportQualityInspection> qualityInspections = this.selectReportQualityInspectionList(inspection);
        if (qualityInspections.size() == 0){
            result = productLineInfo.getProductLineName() + "工位未完成质检！";
        }
        System.out.println("检验前一工位是否已完成质检:" + boxCode + "-" + stationCode + "-" + result);
        return result;
    }


}
