package cn.ezhizao.project.business.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

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.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.domain.BizDaywork;
import cn.ezhizao.project.business.domain.BizProductionPlanDetail;
import cn.ezhizao.project.business.domain.BizStorageRetrieval;
import cn.ezhizao.project.business.service.IBizDayworkService;
import cn.ezhizao.project.business.service.IBizProductionPlanDetailService;
import cn.ezhizao.project.business.service.IBizStorageRetrievalService;
import cn.ezhizao.project.system.domain.SysDeptUser;
import cn.ezhizao.project.system.domain.SysUser;
import cn.ezhizao.project.system.service.ISysDeptUserService;
import cn.ezhizao.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
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-10-11
 */
@RestController
@RequestMapping("/business/storageRetrieval")
public class BizStorageRetrievalController extends BaseController
{
    @Resource
    private IBizStorageRetrievalService bizStorageRetrievalService;

    @Resource
    private IBizDayworkService bizDayworkService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private IBizProductionPlanDetailService bizProductionPlanDetailService;

    /**
     * 查询零存零取列表
     */
    @GetMapping("/list")
    @Log(title = "查询零存零取列表", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizStorageRetrieval bizStorageRetrieval) throws NoSuchFieldException, IllegalAccessException
    {
        startPage();
        if(bizStorageRetrieval.getStartStorageTime()!=null) {
            bizStorageRetrieval.setStartStorageTime(bizStorageRetrieval.getStartStorageTime()+ " 00:00:00");
        }
        if(bizStorageRetrieval.getEndStorageTime()!=null) {
            bizStorageRetrieval.setEndStorageTime(bizStorageRetrieval.getEndStorageTime()+ " 23:59:59");
        }
        if(bizStorageRetrieval.getStartRetrievalTime()!=null) {
            bizStorageRetrieval.setStartRetrievalTime(bizStorageRetrieval.getStartRetrievalTime()+ " 00:00:00");
        }
        if(bizStorageRetrieval.getEndRetrievalTime()!=null) {
            bizStorageRetrieval.setEndRetrievalTime(bizStorageRetrieval.getEndRetrievalTime()+ " 23:59:59");
        }
        List<BizStorageRetrieval> list = bizStorageRetrievalService.getList(bizStorageRetrieval);
        return getDataTable(list);
    }



    /**
     * 新增零存零取
     */
    @Log(title = "新增零取", businessType = BusinessType.UPDATE)
    @PostMapping("/updateFetch")
    public AjaxResult updateFetch(@RequestBody BizStorageRetrieval bizStorageRetrieval) throws NoSuchFieldException, IllegalAccessException
    {
        /**先根据批次号查询批次是否正确
         * 零取时领取批次必须是已经批次完成的
         */
        List <BizDaywork> dayworkList = bizDayworkService.query().eq("lot_code",bizStorageRetrieval.getRetrievalLotCode()).list();
        if(dayworkList.isEmpty()){
            return error("批次号有误或该批次未加工完成，请检查批次号");
        }
        //判断是否是同一个产品
        if(!dayworkList.get(0).getProductId().equals(bizStorageRetrieval.getProductId())){
            return error("只能同产品进行领取");
        }

        bizStorageRetrieval.setRetrievalLotId(dayworkList.get(0).getLotId());
        bizStorageRetrieval.setStatus(1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        bizStorageRetrieval.setRetrievalTime(dateFormat.format(new Date()));
        setTenantId(bizStorageRetrieval);
        return toAjax(bizStorageRetrievalService.saveOrUpdate(bizStorageRetrieval));
    }


    /**
     * 新增零存零取
     */
    @Log(title = "零存查询工段人员", businessType = BusinessType.SELECT)
    @PostMapping("/getDeptUserList")
    public AjaxResult selectDeptUser(@RequestBody SysUser sysUser) throws NoSuchFieldException, IllegalAccessException
    {
        startPage();
        return success(sysUserService.getUserList(sysUser));
    }


    /**
     * 导出零存零取列表
     */
    @PreAuthorize("@ss.hasPermi('business:storageRetrieval:export')")
    @Log(title = "导出零存零取", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizStorageRetrieval bizStorageRetrieval) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizStorageRetrieval);
        List<BizStorageRetrieval> list = bizStorageRetrievalService.getList(bizStorageRetrieval);
        ExcelUtil<BizStorageRetrieval> util = new ExcelUtil<BizStorageRetrieval>(BizStorageRetrieval.class);
        util.exportExcel(response, list, "零存零取数据");
    }

    /**
     * 获取零存零取详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:storageRetrieval:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "获取零存零取详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizStorageRetrievalService.getById(id));
    }

    /**
     * 新增零存零取
     */
    @Log(title = "新增零存", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BizStorageRetrieval bizStorageRetrieval) throws NoSuchFieldException, IllegalAccessException
    {
        //保存之前判断批次是否结束
        List <BizDaywork> dayworkList = bizDayworkService.query().eq("lot_code",bizStorageRetrieval.getLotCode()).eq("status",2).list();
        if(dayworkList.isEmpty()){
            return error("批次号有误或该批次未加工完成，请检查批次号");
        }
        BizDaywork daywork= dayworkList.get(0);
        //再判断该批次是否有零存数据
        List<BizStorageRetrieval>  list=bizStorageRetrievalService.query().eq("lot_id",daywork.getLotId()).list();
        if(!list.isEmpty()){
            return error("该批次已有零存数据");
        }
        //查询计划单信息
        BizProductionPlanDetail productionPlanDetail =bizProductionPlanDetailService.query().eq("id",daywork.getProductionPlanDetailId()).list().get(0);
        bizStorageRetrieval.setLotId(daywork.getLotId());
        bizStorageRetrieval.setProductId(daywork.getProductId());
        bizStorageRetrieval.setTechnologyVersion(productionPlanDetail.getTechnologyVersion());
        bizStorageRetrieval.setProductionPlanDetailId(productionPlanDetail.getId());
        bizStorageRetrieval.setProductDescription(productionPlanDetail.getProductDescription());
        bizStorageRetrieval.setDrawingNumber(productionPlanDetail.getDrawingNumber());
        setTenantId(bizStorageRetrieval);

        return toAjax(bizStorageRetrievalService.save(bizStorageRetrieval));
    }

    /**
     * 修改零存零取
     */
    @Log(title = "修改零存零取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizStorageRetrieval bizStorageRetrieval) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizStorageRetrieval);
        return toAjax(bizStorageRetrievalService.updateById(bizStorageRetrieval));
    }

    /**
     * 删除零存零取
     */
    @PreAuthorize("@ss.hasPermi('business:storageRetrieval:remove')")
    @Log(title = "删除零存零取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizStorageRetrievalService.removeBatchByIds(ids));
    }
}
