package com.hw.qms.controller;

import com.hw.common.core.constant.QmsConstants;
import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.qms.domain.*;
import com.hw.qms.domain.vo.QmsCheckResultDetailVo;
import com.hw.qms.domain.vo.QmsMaterialCheckVo;
import com.hw.qms.service.IMesBaseBarcodeInfoService;
import com.hw.qms.service.IQmsCheckResultDetailService;
import com.hw.qms.service.IQmsCheckResultService;
import com.hw.qms.service.IQmsCheckRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:质检PDA接口
 * @ProjectName:HwMes
 * @Author:xins
 * @Date:2024-01-26 10:18
 * @Version:1.0
 */
@RestController
@RequestMapping("/mobile")
public class QmsMobileController extends BaseController {
    @Autowired
    private IQmsCheckRuleService qmsCheckRuleService;
    @Autowired
    private IQmsCheckResultService qmsCheckResultService;
    @Autowired
    private IQmsCheckResultDetailService qmsCheckResultDetailService;
    @Autowired
    private IMesBaseBarcodeInfoService mesBaseBarcodeInfoService;

    /**
     * 获取质检结果详情
     */
//    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getCheckResultDetail")
    public AjaxResult getCheckResultDetail(QmsMaterialCheckVo qmsMaterialCheckVo) {
        Long checkResultDetailId = qmsMaterialCheckVo.getCheckResultDetailId();
        if (checkResultDetailId != null) {//如果是通过列表页点击进来则根据ID获取
            return success(qmsCheckResultDetailService.selectQmsCheckResultDetailByCheckResultDetailId(checkResultDetailId));
        }

        //扫描物料条码进入获取
        String materialBarcode = qmsMaterialCheckVo.getMaterialBarcode();
        QmsCheckResultDetail qmsCheckResultDetail = qmsCheckResultDetailService.getCheckResultDetail(materialBarcode);
        if (qmsCheckResultDetail != null) {//如果有质检结果，则返回已经保存的质检结果，否则返回需要质检的项目
            return success(qmsCheckResultDetail);
        } else {
            MesBaseBarcodeInfo baseBarcodeInfo = mesBaseBarcodeInfoService.selectMesBaseBarcodeInfoByBarcodeInfo(materialBarcode);
            if (baseBarcodeInfo == null) {
                throw new ServiceException("此物料条码系统中不存在");
            }
            Long materialId = baseBarcodeInfo.getMaterialId();
            QmsCheckRuleDetail queryCheckRuleDetail = new QmsCheckRuleDetail();
            queryCheckRuleDetail.setTargetId(materialId);
            queryCheckRuleDetail.setTargetType(QmsConstants.QMS_CHECK_RULE_DETAIL_TARGET_TYPE_MATERIAL);
            QmsCheckRule qmsCheckRule = qmsCheckRuleService.selectQmsCheckRuleByTarget(queryCheckRuleDetail);
            Long checkResultId = qmsCheckResultService.insertCheckResult(baseBarcodeInfo, qmsCheckRule);
            qmsCheckRule.setCheckResultId(checkResultId);
            return success(qmsCheckRule);
        }
    }


    /**
     * 新增质检结果详情
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "质检结果详情", businessType = BusinessType.INSERT)
    @PostMapping("/saveCheckResultDetail")
    public AjaxResult saveCheckResultDetail(@RequestBody QmsCheckResultDetail qmsCheckResultDetail) {
        if (qmsCheckResultDetail.getCheckResultDetailId() == null) {
            return toAjax(qmsCheckResultDetailService.insertQmsCheckResultDetail(qmsCheckResultDetail));
        } else {
            return toAjax(qmsCheckResultDetailService.updateQmsCheckResultDetail(qmsCheckResultDetail));
        }

    }


    /**
     * 获取质检结果
     */
//    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getCheckResults")
    public TableDataInfo getCheckResults(QmsCheckResult qmsCheckResult) {
        startPage();
        List<QmsCheckResult> list = qmsCheckResultService.selectQmsCheckResultJoinList(qmsCheckResult);
        return getDataTable(list);
    }

    /**
     * 获取批次质检结果信息
     */
//    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getCheckResult")
    public AjaxResult getCheckResult(QmsCheckResult qmsCheckResult) {
        QmsCheckResultDetailVo qmsCheckResultDetailVo = qmsCheckResultDetailService.getCheckResultDetailVo(qmsCheckResult.getCheckResultId());
        int batchAmount = mesBaseBarcodeInfoService.selectBarcodeInfoCountByBatchcode(qmsCheckResult.getMaterialBatch());
        qmsCheckResultDetailVo.setBatchAmount(batchAmount);
        qmsCheckResultDetailVo.setCheckMode(qmsCheckResult.getCheckMode());
        qmsCheckResultDetailVo.setCheckSample(qmsCheckResult.getCheckSample());
        return success(qmsCheckResultDetailVo);
    }

    /**
     * 获取批次质检结果详情列表
     */
//    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getCheckResultDetails")
    public TableDataInfo getCheckResultDetails(QmsCheckResultDetail qmsCheckResultDetail) {
        startPage();
        List<QmsCheckResultDetail> list = qmsCheckResultDetailService.selectQmsCheckResultDetailList(qmsCheckResultDetail);
        return getDataTable(list);
    }


    /**
     * 新增质检结果详情
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "质检结果", businessType = BusinessType.AUDIT)
    @PostMapping("/auditCheckResult")
    public AjaxResult auditCheckResult(@RequestBody QmsCheckResult qmsCheckResult) {
        return toAjax(qmsCheckResultService.auditQmsCheckResult(qmsCheckResult));
    }

}
