package cn.ezhizao.project.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.security.LoginUser;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.domain.BizCheckCategory;
import cn.ezhizao.project.business.domain.BizDayworkItemConsult;
import cn.ezhizao.project.business.domain.BizDayworkItemReject;
import cn.ezhizao.project.business.domain.BizProcessInspecion;
import cn.ezhizao.project.business.service.*;
import cn.ezhizao.project.system.domain.SysRole;
import cn.ezhizao.project.system.domain.SysUserRole;
import cn.ezhizao.project.system.service.ISysUserRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
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;


/**
 * 序检主表Controller
 *
 * @author ezhizao
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/business/inspecion")
public class BizProcessInspecionController extends BaseController
{
    @Resource
    private IBizProcessInspecionService bizProcessInspecionService;
    @Resource
    private IBizDayworkItemRejectService bizDayworkItemRejectService;
    @Resource
    private IBizDayworkItemConsultService bizDayworkItemConsultService;
    @Resource
    private IBizProcessInspectionDetailService bizProcessInspectionDetailService;
    @Resource
    private ISysUserRoleService sysUserRoleService;
    @Resource
    private IBizCheckCategoryService bizCheckCategoryService;

    /**
     * 查询序检主表列表
     */
    @GetMapping("/list")
    @Log(title = "查询序检列表", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProcessInspecion);
        startPage();
        List<BizProcessInspecion> list = bizProcessInspecionService.getList(bizProcessInspecion);
        return getDataTable(list);
    }
    @GetMapping("/getExportCount")
    @Log(title = "查询序检列表", businessType = BusinessType.SELECT)
    public AjaxResult getExportCount(BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProcessInspecion);
//        startPage();
        Long count = bizProcessInspecionService.getCountForExport(bizProcessInspecion);
        return success(count);
    }
    @PostMapping("/save")
    @Log(title = "保存序检详情", businessType = BusinessType.SELECT)
    public AjaxResult save(@RequestBody BizProcessInspecion processInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        List<BizProcessInspecion> newList = new ArrayList<>();
        for(int i = 0;i<=300;i++) {
            BizProcessInspecion bizProcessInspecion = new BizProcessInspecion();
            bizProcessInspecion.setId(null);
            bizProcessInspecion.setUserId(348L);
            bizProcessInspecion.setUserName(""); // 如果 userName 允许为空字符串
            bizProcessInspecion.setNickName("于成林");
            bizProcessInspecion.setCarrierId(1L);
            bizProcessInspecion.setCarrierCode("000001");
            bizProcessInspecion.setDayworkId(1266081332677709824L);
            bizProcessInspecion.setDayworkItemId(1816401806287224835L);
            bizProcessInspecion.setProductionPlanId(0L);
            bizProcessInspecion.setProductionPlanDetailId(2741L);
            bizProcessInspecion.setProductionPlanDetailSubDetailId(0L);
            bizProcessInspecion.setLotId(1813091861567422468L);
            bizProcessInspecion.setLotCode("D24701620030");
            bizProcessInspecion.setProductDescription("意沃汽车_F009D22115_8*66.6");
            bizProcessInspecion.setIsAuto(0); // 假设 isAuto 是整型
            bizProcessInspecion.setTechnologicalProcessId(852L);
            bizProcessInspecion.setDeptId(114L);
            bizProcessInspecion.setProductId(1101L);
            bizProcessInspecion.setRejectNum(0);
            bizProcessInspecion.setExaminingNum(0);
            bizProcessInspecion.setType("patrolInspection");
            bizProcessInspecion.setRemark(""); // 如果 remark 允许为空字符串
            bizProcessInspecion.setIsOutsourcedInspection(0); // 假设 isOutsourcedInspection 是整型
            bizProcessInspecion.setOutsourcedInspectionNo(""); // 如果 outsourcedInspectionNo 允许为空字符串
            bizProcessInspecion.setStatus(0);
            bizProcessInspecion.setReturnReceiptDetailId(1816405033323134977L);
            bizProcessInspecion.setOutsourceOrderDetailId(1266081332677709825L);
            bizProcessInspecion.setOutsourceOrderId(1816401806136229890L);
            bizProcessInspecion.setDisqualificationNum(0);
            bizProcessInspecion.setIsInspectionCarrierChanged(0); // 假设 isInspectionCarrierChanged 是整型
            bizProcessInspecion.setInspectionCarrierChangedTime(null); // 如果允许为 null
            bizProcessInspecion.setProcessNames("热处理-淬火,热处理-清洗,热处理-回火");
            newList.add(bizProcessInspecion);
        }
        bizProcessInspecionService.saveBatch(newList);
        return success();
    }
    @GetMapping("/listInfo")
    @Log(title = "获取序检详情列表", businessType = BusinessType.SELECT)
    public TableDataInfo listInfo(BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        startPage();
        setTenantId(bizProcessInspecion);
        List<BizProcessInspecion> list = bizProcessInspecionService.getInfo(bizProcessInspecion);
        return getDataTable(list);
    }
    //查找当前用户的检查类别权限
    @PostMapping("/getInspectionType")
    @Log(title = "获取当前用户检查类别权限", businessType = BusinessType.SELECT)
    public AjaxResult getInspectionType()
    {
        SysUserRole sysUserRole = new SysUserRole();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        //所有的检测列表
        List<BizCheckCategory> checkCategoryList = bizCheckCategoryService.list();
        //获取当前用户权限
        if(!loginUser.getPermissions().contains("business:processInspection:outsourcedInspection") && loginUser.getUserId() != 1){
            checkCategoryList = checkCategoryList.stream().filter(item -> !item.getCode().equals("outsourcedInspector")).collect(Collectors.toList());
        }
        if(!loginUser.getPermissions().contains("business:processInspection:factoryInspection") &&loginUser.getUserId() != 1){
            checkCategoryList = checkCategoryList.stream().filter(item -> !item.getCode().equals("factoryInspection")).collect(Collectors.toList());
        }
        return success(checkCategoryList);
    }
    @PostMapping("getDayworkItemInspectionDetail")
    @Log(title = "获取报工检查明细", businessType = BusinessType.SELECT)
    public AjaxResult getDayworkItemInspectionDetail(@RequestBody BizProcessInspecion bizProcessInspecion)
    {
       bizProcessInspecion.setProcessInspectionDetails(bizProcessInspectionDetailService.getDetailByProcessInspectionId(bizProcessInspecion.getId())) ;
//        List<BizDayworkItemReject> dayworkItemRejectList = bizDayworkItemRejectService.query().eq("process_inspection_id", bizProcessInspecion.getId()).list();
//       bizProcessInspecion.setDayworkItemRejects(dayworkItemRejectList);
        List<BizDayworkItemConsult> consultList = bizDayworkItemConsultService.query().eq("process_inspection_id", bizProcessInspecion.getId()).list();
        bizProcessInspecion.setDayworkItemConsults(consultList);
        return success(bizProcessInspecion);
    }

    /**
     * 导出序检主表列表
     */
    @Log(title = "导出序检主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProcessInspecion);
        List<BizProcessInspecion> list = bizProcessInspecionService.getList(bizProcessInspecion);
        ExcelUtil<BizProcessInspecion> util = new ExcelUtil<BizProcessInspecion>(BizProcessInspecion.class);
        util.exportExcel(response, list, "序检主表数据");
    }

    /**
     * 获取序检主表详细信息
     */
    @GetMapping(value = "/{id}")
    @Log(title = "获取序检主表详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BizProcessInspecion bizProcessInspecion = bizProcessInspecionService.getProcessInspectionById(id).get(0);
       bizProcessInspecion.setProcessInspectionDetails(bizProcessInspectionDetailService.getDetailByProcessInspectionId(id));
       bizProcessInspecion.setConsultList(bizDayworkItemConsultService.query().eq("process_inspection_id", id).list());
        return success(bizProcessInspecion);
    }

    /**
     * 新增序检主表
     */
    @PreAuthorize("@ss.hasPermi('business:inspecion:add')")
    @Log(title = "新增序检主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProcessInspecion);
        return toAjax(bizProcessInspecionService.save(bizProcessInspecion));
    }

    /**
     * 修改序检主表
     */
    @PreAuthorize("@ss.hasPermi('business:inspecion:edit')")
    @Log(title = "修改序检主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProcessInspecion bizProcessInspecion) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizProcessInspecion);
        return toAjax(bizProcessInspecionService.updateById(bizProcessInspecion));
    }

    /**
     * 删除序检主表
     */
    @PreAuthorize("@ss.hasPermi('business:inspecion:remove')")
    @Log(title = "删除序检主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizProcessInspecionService.removeBatchByIds(ids));
    }
}
