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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IModulePackageTaskService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageRecord;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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.LocaleUtils;
import cn.estsh.impp.framework.boot.util.ResultBean;
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 xiangming.liao
 * @date 2021年03月23日 11:03
 */
@RestController
@Api(tags = "组件包装作业")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/module-package-task")
public class ModulePackageTaskController extends MesPcnBaseController {

    @Autowired
    private IModulePackageTaskService iModulePackageTaskService;


    @GetMapping(value = "/work-order/by-center/query")
    @ApiOperation(value = "通过产线查询工单顺序最先启动状态的工单", notes = "通过产线查询工单顺序最先启动状态的工单")
    public ResultBean queryWorkOrderByCenter(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功")).setResultObject(iModulePackageTaskService.queryWorkOrderByCenter(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/container/by-scan-barcode/query")
    @ApiOperation(value = "通过扫描条码，查询容器，加载数据", notes = "通过扫描条码，查询容器，加载数据")
    public ResultBean queryContainerByScanBarcode(String workOrderNo, String barcode, String organizeCode) {
        try {
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功")).setResultObject(iModulePackageTaskService.queryContainerByScanBarcode(workOrderNo, barcode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/complete-qty/insert")
    @ApiOperation(value = "输入完工数量点击确定按钮", notes = "输入完工数量点击确定按钮")
    public ResultBean insertCompleteQty(@RequestBody MesPackageRecord packageRecord) {
        try {

            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功"))
                    .setResultObject(iModulePackageTaskService.insertCompleteQty(packageRecord))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/package-record/delete")
    @ApiOperation(value = "点击删除按钮，删除包装记录", notes = "点击删除按钮，删除包装记录")
    public ResultBean deletePackageRecord(Long ids[], String organizeCode, String modifyUser) {
        try {
            iModulePackageTaskService.deletePackageRecord(ids, organizeCode, modifyUser);
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功"))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/do-complete-package/insert")
    @ApiOperation(value = "点击完成包装按钮", notes = "点击完成包装按钮")
    public ResultBean doCompletePackage(String workOrderNo, String partNo, String barcode, String organizeCode, String modifyUser) {
        try {
            iModulePackageTaskService.doCompletePackage(workOrderNo, partNo, barcode, organizeCode, modifyUser);
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功"))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
