package cn.ezhizao.project.business.controller;

import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
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.*;
import cn.ezhizao.project.business.service.IBizLotService;
import cn.ezhizao.project.business.service.IBizProductionPlanDetailService;
import cn.ezhizao.project.business.service.IBizTechnologicalProcessDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 生产计划明细Controller
 *
 * @author ruoyi
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/business/productionPlanDetail")
public class BizProductionPlanDetailController extends BaseController {
    @Resource
    private IBizProductionPlanDetailService bizProductionPlanDetailService;

    @Resource
    private IBizTechnologicalProcessDetailService bizTechnologicalProcessDetailService;

    @Resource
    private IBizLotService bizLotService;
    @Resource
    HttpServletRequest request;

    /**
     * 查询生产计划明细列表
     */
//    @PreAuthorize("@ss.hasPermi('business:productionBatch:list')")
    @GetMapping("/list")
    public TableDataInfo list( BizProductionPlanDetail data) throws NoSuchFieldException, IllegalAccessException {
//        String tenantId = request.getHeader("tenantId");
//        data.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProductionPlanDetail> list = bizProductionPlanDetailService.getList(data);
        return getDataTable(list);
    }
    /**
     * 查询生产计划明细列表
     */
    @GetMapping("/getList")
    public TableDataInfo getInfo( BizProductionPlanDetail data) throws NoSuchFieldException, IllegalAccessException {
//        String tenantId = request.getHeader("tenantId");
//        data.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProductionPlanDetail> list = bizProductionPlanDetailService.getInfo(data);
        int index = 0;
        for (int i =0; i < list.size(); i++) {
             index = 0;
            for (int j = 0; j<list.get(i).getDayWorkList().size(); j++) {
                if(list.get(i).getDayWorkList().get(j).getDeptId().equals(data.getDeptId())) {
                    index ++;
                }

            }
            list.get(i).setDayWorkLot(index);
        }
        return getDataTable(list);
    }

    /**
     * 获取生产计划明细详细信息
     */


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProductionPlanDetailService.getById(id));
    }

    /**
     * 新增生产计划明细
     */
    @PreAuthorize("@ss.hasPermi('business:productionPlanDetail:add')")
    @Log(title = "生产计划明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProductionPlanDetail bizProductionPlanDetail) throws NoSuchFieldException, IllegalAccessException {
//        String tenantId = request.getHeader("tenantId");
//        bizProductionPlanDetail.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizProductionPlanDetailService.save(bizProductionPlanDetail));
    }

    /**
     * 修改生产批号状态
     */
    @Log(title = "生产计划明细", businessType = BusinessType.UPDATE)
    @PutMapping("/updateLotCodeStatus")
    public AjaxResult updateLotCodeStatus(@RequestBody BizProductionPlanDetail bizProductionPlanDetail)  {
//        String tenantId = request.getHeader("tenantId");
//        bizProductionPlanDetail.setTenantId(Long.valueOf(tenantId));

        return toAjax(bizProductionPlanDetailService.updateLotCodeStatus(bizProductionPlanDetail));
    }
    /**
     * 获取工艺版本
     */
    @GetMapping("/getTechnologicalVersion")
    public AjaxResult getTechnologicalVersion(BizTechnologicalProcess bizTechnologicalProcess) {
        return success(bizProductionPlanDetailService.getTechnologicalVersion(bizTechnologicalProcess));
    }

    @GetMapping("/getTechnologicalProcessDetailList")
    public AjaxResult getTechnologicalProcessDetailList(BizTechnologicalProcessDetail bizTechnologicalProcessDetail) {
        QueryWrapper<BizTechnologicalProcessDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("technological_process_id", bizTechnologicalProcessDetail.getTechnologicalProcessId());
        return success(bizTechnologicalProcessDetailService.list(queryWrapper));
    }

    /**
     * 修改生产计划批号状态
     */
    @PreAuthorize("@ss.hasPermi('business:productionPlanDetail:edit')")
    @Log(title = "生产计划明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProductionPlanDetail bizProductionPlanDetail) throws NoSuchFieldException, IllegalAccessException {
         //修改生产版本，向新表存历史记录，同时修改biz_lot表里未投产的
        BizChangeTechnologicalVersion bizChangeTechnologicalVersion = new BizChangeTechnologicalVersion();
        bizChangeTechnologicalVersion.setProductionPlanDetailId(bizProductionPlanDetail.getId());
        bizChangeTechnologicalVersion.setOldTechnologicalProcessId(bizProductionPlanDetail.getOldTechnologicalProcessId());
        bizChangeTechnologicalVersion.setOldTechnologyVersion(bizProductionPlanDetail.getOldTechnologyVersion());
        bizChangeTechnologicalVersion.setNewTechnologyVersion(bizProductionPlanDetail.getTechnologyVersion());
        bizChangeTechnologicalVersion.setNewTechnologicalProcessId(bizProductionPlanDetail.getTechnologicalProcessId());
        List<BizLot> notStartList = bizLotService.getNotStartList(bizProductionPlanDetail);
        notStartList.forEach(item ->
        {item.setTechnologyVersion(bizProductionPlanDetail.getTechnologyVersion());
                item.setTechnologicalProcessId(bizProductionPlanDetail.getTechnologicalProcessId());});
        //修改生产计划表的版本
        BizProductionPlanDetail productionPlanDetail = bizProductionPlanDetailService.getById(bizProductionPlanDetail.getId());
        productionPlanDetail.setTechnologicalProcessId(bizProductionPlanDetail.getTechnologicalProcessId());
        productionPlanDetail.setTechnologyVersion(bizProductionPlanDetail.getTechnologyVersion());
        bizProductionPlanDetailService.updateById(productionPlanDetail);
        return toAjax(bizLotService.updateBatchById(notStartList));
    }

    /**
     * 删除生产计划明细
     */
    @PreAuthorize("@ss.hasPermi('business:productionPlanDetail:remove')")
    @Log(title = "生产计划明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizProductionPlanDetailService.removeBatchByIds(ids));
    }
}
