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

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.constant.ProduceManageConstant;
import com.deer.wms.produce.manage.constant.ProduceManagePublicMethod;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MtAloneProcessService;
import com.deer.wms.project.seed.annotation.OperateLog;
import com.deer.wms.project.seed.constant.SystemManageConstant;
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.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("/mt/alone/processs")
public class MtAloneProcessController {

    @Autowired
    private MtAloneProcessService mtAloneProcessService;

    /**工序处理界面，由前端获取工序列表对应行工序数据，完善后保存至工序库表**/
    @OperateLog(description = "添加/处理工序", type = "增加")
    @ApiOperation(value = "添加/处理工序", notes = "添加/处理工序")
    @PostMapping("/add")
    public Result add(@RequestBody MtAloneProcess mtAloneProcess, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult( CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        Date date = new Date();
        mtAloneProcess.setCreateTime(date);
        mtAloneProcess.setUpdateTime(date);
        mtAloneProcess.setOperatorId(currentUser.getUserId());
        mtAloneProcess.setVersion("1.1");
        mtAloneProcess.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        mtAloneProcess.setCode(ProduceManagePublicMethod.creatUniqueCode("GX"));
        mtAloneProcess.setLoss(mtAloneProcess.getAfterLen()/mtAloneProcess.getOriginalLen());
        mtAloneProcess.setMerchandiserId(currentUser.getUserId());
        mtAloneProcess.setCompanyId(currentUser.getCompanyId());
        mtAloneProcessService.save(mtAloneProcess);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "删除xxx", type = "删除")
    @ApiOperation(value = "删除xxx", notes = "删除xxx")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        mtAloneProcessService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }
    
    @OperateLog(description = "修改xxx", type = "更新")
    @ApiOperation(value = "修改xxx", notes = "修改xxx")
    @PostMapping("/update")
    public Result update(@RequestBody MtAloneProcess mtAloneProcess) {
        mtAloneProcess.setUpdateTime(new Date());
        mtAloneProcessService.update(mtAloneProcess);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Integer id) {
        MtAloneProcess mtAloneProcess = mtAloneProcessService.findById(id);
        return ResultGenerator.genSuccessResult(mtAloneProcess);
    }

    /**
     * hy
     * 暂时没用
     * 用于批次生产加工新建页面下方的工序列表展示,获取工序bom和工序，如果没有工序则新建对应的工序并初步赋值
     * @param params
     * @param currentUser
     * @return
     */
    @OperateLog(description = "根据产品bom查询工序", type = "查询")
    @ApiOperation(value = "根据产品bom查询工序", notes = "根据产品bom查询工序")
    @GetMapping("/processListByProductBom")
    public Result processListByProductBom(ProcessBomParams 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<MtAloneProcess> list = mtAloneProcessService.findProcessListByProductBom(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @OperateLog(description = "根据批次产品加工单id查询工序", type = "查询")
    @ApiOperation(value = "根据批次产品加工单id查询工序", notes = "根据批次产品加工单id查询工序")
    @GetMapping("/processListByProductProcess")
    public Result processListByProductProcess(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<MtAloneProcessVo> list = mtAloneProcessService.findProcessVoListByProductProcess(params);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * hy正在用
     * 点击加工处理页面中工序列表最后一列查看或处理按钮，
     * 由工序id获取该工序详情及包含的物料信息
     * @param params
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "工序详情查询", notes = "工序详情查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId", value = "工序id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "query", dataType = "int", required = false)

    })
    @OperateLog(description = "工序详情查询", type = "查询")
    @GetMapping("/query/process/detail")
    public Result queryProcessDetail(MtAloneProcessParams params, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        params.setCompanyId(currentUser.getCompanyId());
        ProcessHo processHo = mtAloneProcessService.queryProcessDetail(params);
        return ResultGenerator.genSuccessResult(processHo);
    }

    /**
     * hy停用
     * 工序处理完毕时，点击提交同时更新工序信息、保存物料信息、更新物料库存信息
     * @param processAndProcMat
     * @param currentUser
     * @return
     */
    @ApiOperation(value = "工序及物料信息保存", notes = "工序及物料信息保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processAndProcMat", value = "工序及物料", paramType = "body", dataType = "ProcessAndProcMat", required = true)
    })
    @OperateLog(description = "工序及物料信息保存", type = "更新、保存")
    @PostMapping("/saveProcAndProcMat")
    public Result saveProcAndProcMat(@RequestBody ProcessAndProcMat processAndProcMat, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser==null){
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null );
        }

        mtAloneProcessService.saveProcAndProcMat(processAndProcMat, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * hy正在用
     * 工序处理完毕点提交按钮后，同时更新工序和物料信息
     * @param processHo
     * @return
     */
    @ApiOperation(value = "工序详情更新", notes = "工序详情更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processHo", value = "工序及物料", paramType = "body", dataType = "ProcessHo", required = true)
    })
    @OperateLog(description = "工序详情更新", type = "更新")
    @PostMapping("/updateProcess")
    public Result updateProcess(@RequestBody ProcessHo processHo) {
        mtAloneProcessService.updateProcess(processHo);
        return ResultGenerator.genSuccessResult();
    }
}
