package com.hw.qms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hw.qms.domain.MesBaseMaterialInfo;
import com.hw.qms.domain.QmsCheckProject;
import com.hw.qms.domain.QmsCheckRuleDetail;
import com.hw.qms.domain.QmsCheckRuleProject;
import com.hw.qms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.QmsCheckRule;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

/**
 * 检验规则维护Controller
 *
 * @author xins
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/checkrule")
public class QmsCheckRuleController extends BaseController
{
    @Autowired
    private IQmsCheckRuleService qmsCheckRuleService;

    @Autowired
    private IQmsCheckRuleDetailService qmsCheckRuleDetailService;

    @Autowired
    private IQmsCheckProjectService qmsCheckProjectService;

    @Autowired
    private IMesBaseMaterialInfoService mesBaseMaterialInfoService;

    /**
     * 查询检验规则维护列表
     */
    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/list")
    public TableDataInfo list(QmsCheckRule qmsCheckRule)
    {
        startPage();
        List<QmsCheckRule> list = qmsCheckRuleService.selectQmsCheckRuleList(qmsCheckRule);
        return getDataTable(list);
    }

    /**
     * 导出检验规则维护列表
     */
    @RequiresPermissions("qms:checkrule:export")
    @Log(title = "检验规则维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsCheckRule qmsCheckRule)
    {
        List<QmsCheckRule> list = qmsCheckRuleService.selectQmsCheckRuleList(qmsCheckRule);
        ExcelUtil<QmsCheckRule> util = new ExcelUtil<QmsCheckRule>(QmsCheckRule.class);
        util.exportExcel(response, list, "检验规则维护数据");
    }

    /**
     * 获取检验规则维护详细信息
     */
    @RequiresPermissions("qms:checkrule:query")
    @GetMapping(value = "/{checkRuleId}")
    public AjaxResult getInfo(@PathVariable("checkRuleId") Long checkRuleId)
    {
        return success(qmsCheckRuleService.selectQmsCheckRuleByCheckRuleId(checkRuleId));
    }

    /**
     * 新增检验规则维护
     */
    @RequiresPermissions("qms:checkrule:add")
    @Log(title = "检验规则维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsCheckRule qmsCheckRule)
    {
        return toAjax(qmsCheckRuleService.insertQmsCheckRule(qmsCheckRule));
    }

    /**
     * 修改检验规则维护
     */
    @RequiresPermissions("qms:checkrule:edit")
    @Log(title = "检验规则维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsCheckRule qmsCheckRule)
    {
        return toAjax(qmsCheckRuleService.updateQmsCheckRule(qmsCheckRule));
    }

    /**
     * 删除检验规则维护
     */
    @RequiresPermissions("qms:checkrule:remove")
    @Log(title = "检验规则维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkRuleIds}")
    public AjaxResult remove(@PathVariable Long[] checkRuleIds)
    {
        return toAjax(qmsCheckRuleService.deleteQmsCheckRuleByCheckRuleIds(checkRuleIds));
    }






    /**
     * 查询已选择规则明细(物料)
     */
    @RequiresPermissions("qms:checkrule:add")
    @GetMapping("/getCheckProjects")
    public AjaxResult getCheckProjects(QmsCheckProject checkProject)
    {
        startPage();
        List<QmsCheckProject> checkProjects = qmsCheckProjectService.selectQmsCheckProjectList(checkProject);
        return success(checkProjects);
    }




    /**
     * 查询检验规则未配置物料信息列表
     */
    @RequiresPermissions("qms:checkrule:checkRuleDetail")
    @GetMapping("/selectMaterialInfos4AllocateCheckRule")
    public TableDataInfo selectMaterialInfos4AllocateCheckRule(MesBaseMaterialInfo mesBaseMaterialInfo)
    {
        startPage();
        List<MesBaseMaterialInfo> list = mesBaseMaterialInfoService.selectMaterialInfos4AllocateCheckRule(mesBaseMaterialInfo);
        return getDataTable(list);
    }


    /**
     * 查询已选择规则明细(物料)
     */
    @RequiresPermissions("qms:checkrule:checkRuleDetail")
    @GetMapping("/selectCheckRuleDetailList")
    public TableDataInfo selectCheckRuleDetailList(QmsCheckRuleDetail qmsCheckRuleDetail)
    {
        startPage();
        List<QmsCheckRuleDetail> checkRuleDetails = qmsCheckRuleDetailService.selectQmsCheckRuleDetailJoinList(qmsCheckRuleDetail);
        return getDataTable(checkRuleDetails);
    }

    /**
     * 配置检验规则明细（物料）
     */
    @RequiresPermissions("qms:checkrule:checkRuleDetail")
    @Log(title = "检验规则维护", businessType = BusinessType.ALLOCATE)
    @PutMapping("/allocateCheckRuleDetails")
    public AjaxResult allocateCheckRuleDetails(@RequestBody List<QmsCheckRuleDetail> checkRuleDetailList)
    {
        return toAjax(qmsCheckRuleService.batchInsertCheckRuleDetail(checkRuleDetailList));
    }


    /**
     * 删除检验规则配置的物料信息
     */
    @RequiresPermissions("qms:checkrule:checkRuleDetail")
    @Log(title = "检验规则维护", businessType = BusinessType.UNALLOCATE)
    @PostMapping("/unallocateCheckRuleDetails")
    public AjaxResult unallocateCheckRuleDetails(@RequestBody List<QmsCheckRuleDetail> checkRuleDetailList)
    {
        return toAjax(qmsCheckRuleDetailService.deleteQmsCheckRuleDetails(checkRuleDetailList));
    }




    /**
     * 查询检验规则未配置项目列表
     */
    @RequiresPermissions("qms:checkrule:checkRuleProject")
    @GetMapping("/selectProjects4AllocateCheckRule")
    public TableDataInfo selectProjects4AllocateCheckRule(QmsCheckProject qmsCheckProject)
    {
        startPage();
        List<QmsCheckProject> list = qmsCheckProjectService.selectQmsCheckProjects4AllocateCheckRule(qmsCheckProject);
        return getDataTable(list);
    }



//    /**
//     * 查询检验规则已分配项目
//     */
//    @RequiresPermissions("qms:checkrule:checkRuleProject")
//    @GetMapping("/selectCheckRuleProjects")
//    public TableDataInfo selectCheckRuleProjects(QmsCheckRuleProject qmsCheckRuleProject)
//    {
//        startPage();
//        List<QmsCheckRuleProject> checkRuleProjects = qmsCheckRuleProjectService.selectQmsCheckRuleProjectJoinList(qmsCheckRuleProject);
//        return getDataTable(checkRuleProjects);
//    }
//
//    /**
//     * 配置检验规则项目
//     */
//    @RequiresPermissions("qms:checkrule:checkRuleProject")
//    @Log(title = "检验规则维护", businessType = BusinessType.ALLOCATE)
//    @PutMapping("/allocateCheckRuleProjects")
//    public AjaxResult allocateCheckRuleProjects(List<QmsCheckRuleProject> qmsCheckRuleProjects)
//    {
//        return toAjax(qmsCheckRuleService.batchInsertCheckRuleProject(qmsCheckRuleProjects));
//    }
//
//
//    /**
//     * 删除检验规则配置的项目信息
//     */
//    @RequiresPermissions("qms:checkrule:checkRuleProject")
//    @Log(title = "检验规则维护", businessType = BusinessType.UNALLOCATE)
//    @PostMapping("/unallocateCheckRuleProjects")
//    public AjaxResult unallocateCheckRuleProjects(List<QmsCheckRuleProject> checkRuleProjects)
//    {
//        return toAjax(qmsCheckRuleProjectService.deleteQmsCheckRuleProjects(checkRuleProjects));
//    }

}
