package com.deer.wms.produce.manage.web;

import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MtAloneProcessService;
import com.deer.wms.produce.manage.service.ProcessBomService;
import com.deer.wms.produce.manage.service.ProductProcessBatchDetService;
import com.deer.wms.produce.manage.service.ProductProcessService;
import com.deer.wms.project.seed.annotation.OperateLog;
import com.deer.wms.project.seed.core.result.CommonCode;
import com.deer.wms.project.seed.core.result.Result;
import com.deer.wms.project.seed.core.result.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;


/**
* Created by hy on 2019/07/19.
*/
@Api(description = "生产加工接口")
@RestController
@RequestMapping("/product/processs")
public class ProductProcessController {

    @Autowired
    private ProductProcessService productProcessService;

    @Autowired
    private MtAloneProcessService processService;

    @Autowired
    private ProductProcessBatchDetService productProcessBatchDetService;

    /**
     * hy暂时没用
     * 生成某种产品某一批次的生产加工单
     * 需要调用对应产品bom
     * @param productProcess
     * @param currentUser
     * @return
     */
    @OperateLog(description = "添加批次生产加工单", type = "增加")
    @ApiOperation(value = "添加批次生产加工单", notes = "添加批次生产加工单")
    @PostMapping("/add")
    public Result add(@RequestBody ProductProcess productProcess, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        productProcessService.save(productProcess, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy暂没用
     * 删除批次生产加工单，顺带删除其下的工序、物料信息
     * @param produceProcessId 批次生产加工单id
     * @param currentUser
     * @return
     */
    @OperateLog(description = "删除批次生产加工单", type = "删除")
    @ApiOperation(value = "删除批次生产加工单", notes = "删除批次生产加工单")
    @DeleteMapping("/delete/{produceProcessId}")
    public Result delete(@PathVariable Integer produceProcessId, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        //productProcessService.deleteRelevantById(produceProcessId, currentUser);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "修改xxx", type = "更新")
    @ApiOperation(value = "修改xxx", notes = "修改xxx")
    @PostMapping("/update")
    public Result update(@RequestBody ProductProcess productProcess) {
        productProcessService.update(productProcess);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        ProductProcess productProcess = productProcessService.findById(id);
        return ResultGenerator.genSuccessResult(productProcess);
    }

    /**
     * hy正在用
     * 批次生产加工单分页查询
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "批次生产加工单分页查询", notes = "批次生产加工单分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createDate", value = "创建时间", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String", required = false)

    })
    @OperateLog(description = "批次生产加工单分页查询", type = "查询")
    @GetMapping("/list")
    public Result list(ProductProcessParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<ProductProcessVo> list = productProcessService.findList(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy停用
     * 新建批次产品加工完毕时，点击提交同时保存产品加工和工序信息
     * @param productAndProcess
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "批次生产加工及工序保存", notes = "批次生产加工及工序保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productAndProcess", value = "批次生产加工及工序", paramType = "body", dataType = "ProductAndProcess", required = true)
    })
    @OperateLog(description = "批次生产加工及工序保存", type = "保存")
    @PostMapping("/saveProdAndProc")
    public Result saveProdAndProc(@RequestBody ProductAndProcess productAndProcess, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        productProcessService.saveProdAndProc(productAndProcess, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 点击批次生产加工单列表最后一列查看或处理按钮，
     * 获取该批次生产加工单详情
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "批次生产加工单详情查询", notes = "批次生产加工单详情查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessId", value = "批次生产加工单id", paramType = "query", dataType = "int", required = true)
    })
    @OperateLog(description = "批次生产加工单详情查询", type = "查询")
    @GetMapping("/query/product/process/detail")
    public Result queryProductProcessDetail(ProductProcessParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        ProductProcessHo productProcessHo = productProcessService.queryProductProcessDetail(params);
        return ResultGenerator.genSuccessResult(productProcessHo);
    }

    /**
     * hy正在用
     * 新建批次产品加工时，选择产品bom后，
     * 生成加工单、工序、物料信息并对其初步赋值，用于表格展示
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "初始化批次生产加工单信息", notes = "初始化批次生产加工单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productBomId", value = "产品bom的id", paramType = "query", dataType = "int", required = true)

    })
    @OperateLog(description = "初始化批次生产加工单信息", type = "查询")
    @GetMapping("/initialProdProcByProdProcBom")
    public Result initialProdProcByProdProcBom(ProductProcessBomParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        ProductProcessHo productProcessHo = productProcessService.initialProdProcByProdProcBom(params);
        return ResultGenerator.genSuccessResult(productProcessHo);
    }


    /**
     * hy正在用
     * 新建批次产品加工时，点击提交按钮在库表中同时保存初步的批次加工单、批次明细、工序、物料信息
     * @param productProcessHo
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "新增批次生产加工单", notes = "新增批次生产加工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessHo", value = "批次生产加工信息", paramType = "body", dataType = "ProductProcessHo", required = true)
    })
    @OperateLog(description = "新增批次生产加工单", type = "保存")
    @PostMapping("/addProductProcess")
    public Result addProductProcess(@RequestBody ProductProcessHo productProcessHo, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        productProcessService.addProduceProcess(productProcessHo, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 批次生产加工单处理完毕点保存按钮后，仅更新批次加工单信息
     * @param productProcessHo
     * @return
     */
    @ApiOperation(value = "批次加工单详情更新", notes = "批次加工单详情更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessHo", value = "批次加工单", paramType = "body", dataType = "ProductProcessHo", required = true)
    })
    @OperateLog(description = "批次加工单详情更新", type = "更新")
    @PostMapping("/updateProductProcess")
    public Result updateProductProcess(@RequestBody ProductProcessHo productProcessHo) {
        ProductProcess productProcess = new ProductProcess();
        productProcessHo.setUpdateTime(new Date());
        BeanUtils.copyProperties(productProcessHo, productProcess);
        productProcessService.update(productProcess);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用（打卷系统）
     * 根据批次加工单id获取其下所有的工序和明细
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "批次生产加工单下的工序和批次明细查询", notes = "批次生产加工单下的工序和批次明细查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessId", value = "批次生产加工单id", paramType = "query", dataType = "int", required = true)
    })
    @OperateLog(description = "批次生产加工单下的工序和批次明细查询", type = "查询")
    @GetMapping("/processAndDetByProductProcess")
    public Result processAndDetByProductProcess(ProductProcessParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        ProcessAndDet processAndDet = new ProcessAndDet();
        //获取工序
        List<MtAloneProcessVo> processVoList = processService.findProcessVoListByProductProcess(params);
        //获取批次明细
        List<ProductProcessBatchDet> productProcessBatchDetList = productProcessBatchDetService.findListByProductProcess(params);

        processAndDet.setProductProcessId(params.getProductProcessId());//批次加工单id赋值
        processAndDet.setProcessVoList(processVoList);//所有工序赋值
        processAndDet.setProductProcessBatchDetList(productProcessBatchDetList);//所有批次明细赋值
        return ResultGenerator.genSuccessResult(processAndDet);
    }

    /**
     * hy正在用（打卷系统）
     * 根据批次加工单id、工序id、明细id，获取批次加工单、工序、明细信息，用于打卷页面信息展示
     * @param condition
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "根据批次加工单id、工序id、批次明细id，获取批次加工单、工序、批次明细信息", notes = "根据批次加工单id、工序id、批次明细id，获取批次加工单、工序、批次明细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productProcessId", value = "批次生产加工单id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "processId", value = "工序id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "batchDetId", value = "批次明细id", paramType = "query", dataType = "int", required = true)
    })
    @OperateLog(description = "根据批次加工单id、工序id、批次明细id，获取批次加工单、工序、批次明细信息", type = "查询")
    @GetMapping("/productProcessBatchDetInfoByProductProcess")
    public Result productProcessBatchDetInfoByProductProcess(ProductProcessBatchDetCondition condition, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        condition.setCompanyId(currentUser.getCompanyId());
        ProductProcessBatchDetInfo productProcessBatchDetInfo = new ProductProcessBatchDetInfo();
        //获取批次加工单
        ProductProcessParams productProcessParams = new ProductProcessParams();
        BeanUtils.copyProperties(condition, productProcessParams);
        ProductProcessVo productProcessVo = productProcessService.findProductProcessById(productProcessParams);
        productProcessBatchDetInfo.setProductProcessVo(productProcessVo);
        //获取工序
        MtAloneProcessParams processParams = new MtAloneProcessParams();
        BeanUtils.copyProperties(condition, processParams);
        MtAloneProcessVo processVo = processService.findProcessById(processParams);
        productProcessBatchDetInfo.setProcessVo(processVo);
        //获取批次明细
        ProductProcessBatchDetParams batchDetParams = new ProductProcessBatchDetParams();
        BeanUtils.copyProperties(condition, batchDetParams);
        ProductProcessBatchDet batchDet = productProcessBatchDetService.findBatchDetById(batchDetParams);
        productProcessBatchDetInfo.setProductProcessBatchDet(batchDet);

        return ResultGenerator.genSuccessResult(productProcessBatchDetInfo);
    }

}
