package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.GenerateWorkOrderDto;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderQueryDto;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.RequestModel;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/12 14:39
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/work-order")
@Api(tags = "松下生产工单")
public class SxWorkOrderExtController extends MesPcnBaseController {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;


    @ApiOperation("生成工单")
    @PostMapping("generate")
    public ResultBean generateWorkOrder(@RequestBody GenerateWorkOrderDto workOrderDto){
        LOGGER.info("生成工单 输入参数 {}",workOrderDto);
        try {
            ValidatorBean.beginValid(workOrderDto)
                    .notNull("partNo",workOrderDto.getPartNo())
                    .notNull("partNameRdd",workOrderDto.getPartNameRdd())
                    .notNull("qty",workOrderDto.getQty())
                    .notNull("workCenterCode",workOrderDto.getWorkCenterCode())
                    .notNull("workCenterName",workOrderDto.getWorkCenterName())
                    .notNull("startTime",workOrderDto.getStartTime())
                    .notNull("endTime",workOrderDto.getEndTime());
            workOrderExtService.insertManualGenerate(workOrderDto);
            return ResultBean.success();
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }

    }



    @ApiOperation("调整工单")
    @PostMapping("adjust")
    public ResultBean adjustWorkOrder(@RequestBody GenerateWorkOrderDto generateWorkOrderDto){
        try {

            workOrderExtService.adjustWorkOrder(generateWorkOrderDto);
            return ResultBean.success();
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query-list-page")
    @ApiOperation(value = "生产工单分页查询")
    public ResultBean geWorkOrderListPage(WorkOrderQueryDto workOrderQueryDto, Pager pager) {
        try {

            // 生产工单分页列表
            return ResultBean.success("生产工单分页查询成功")
                    .setListPager(workOrderExtService.getListPageByCondition(workOrderQueryDto, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }




    @PutMapping(value = "/update-order-status")
    @ApiOperation(value = "生产工单状态批量调整")
    public ResultBean updateWorkOrderStatus(@RequestBody RequestModel requestModel) {
        try {
            List<String> orderNoList = null;
            Integer workOrderStatus = null;
            if (null != requestModel) {
                orderNoList = requestModel.getOrderNoList();
                workOrderStatus = requestModel.getDestStatus();
            }
            // 数据校验
            ValidatorBean.checkNotNull(workOrderStatus, "工单状态不能为空");
            ValidatorBean.checkNotNull(orderNoList, "工单号不能为空");

            workOrderExtService.updateWorkOrderStatus(workOrderStatus, orderNoList);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    //绑定关键件；缺件记录；打包；装托
    @GetMapping(value = "ext-window/query-list-page")
    @ApiOperation(value = "开窗生产工单分页查询")
    public ResultBean queryWindowWorkOrderExtListPage(WorkOrderQueryDto workOrderQueryDto, Pager pager) {
        try {
            ValidatorBean.beginValid(workOrderQueryDto)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workOrderQueryDto.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, workOrderQueryDto.getWorkCenterCode());
            return ResultBean.success("生产工单分页查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workOrderExtService.queryWindowWorkOrderExtListPage(workOrderQueryDto, pager));
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    //绑定关键件；缺件记录；打包；装托
//    @PostMapping(value = "/package-sn-unbind")
//    @ApiOperation(value = "包装条码解绑")
//    public ResultBean packageSnUnbind(String packageSn) {
//        try {
//            workOrderExtService.packageSnUnbind(packageSn, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
//            return ResultBean.success("包装条码解绑成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
//        } catch (ImppBusiException imppException) {
//            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
//            return ResultBean.fail(imppException);
//        } catch (Exception e) {
//            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
//        }
//    }

}
