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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxReworkRangeService;
import cn.estsh.i3plus.ext.mes.pojo.model.BatchIdModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxPackSnReportModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxReworkRangeModel;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-06-20 9:09
 */
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES )
@Api(tags = "返工范围控制层")
public class SxMesReworkRangeController {

    @Autowired
    private ISxReworkRangeService reworkRangeService;

    @GetMapping(value = "/sx-ext/rework-range/query")
    @ApiOperation(value = "按条件分页查询返工范围")
    public ResultBean queryReworkRangeByPager(SxReworkRangeModel reworkRange, Pager pager) {
        try {
            reworkRange.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reworkRangeService.queryMesReworkRangeByPager(reworkRange, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-ext/rework-range/standard-work-order/pack-sn/query")
    @ApiOperation(value = "正常工单包装条码查询报表")
    public ResultBean queryStandardWorkOrderPackSnPager(SxPackSnReportModel model, Pager pager) {
        try {
            ValidatorBean.checkNotNull(model.getOutWcTimeStart(), "请输入下线日期起始");
            ValidatorBean.checkNotNull(model.getOutWcTimeEnd(), "请输入下线日期截至");
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reworkRangeService.queryStandardWorkOrderPackSnPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-ext/rework-range/insert")
    @ApiOperation(value = "添加返工范围")
    public ResultBean insertReworkRangeList(@RequestBody List<SxPackSnReportModel> packSnReportModelList) {
        try {
            reworkRangeService.insertMesReworkRange(AuthUtil.getOrganize().getOrganizeCode(), packSnReportModelList);
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/sx-ext/rework-range/status-update")
    @ApiOperation(value = "根据id启用或禁用返工范围信息")
    public ResultBean updateReworkRangeStatusById(@RequestBody BatchIdModel batchIdModel) {
        try {
            ValidatorBean.checkNotNull(batchIdModel.getIds(), "请选择需要操作的资源");
            //ids = ConvertBean.modelSafeArrayNumber(ids, true);
            if (batchIdModel.getIds().length == 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            reworkRangeService.updateConfigValidById((Long[]) ConvertUtils.convert(batchIdModel.getIds(), Long.class), batchIdModel.getStatus(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-ext/rework-range/standard-work-order-lot/pack-sn/query")
    @ApiOperation(value = "QMS调MES 获取工单某一批次条码")
    public ResultBean queryStandardWorkOrderPackSnListWithQms(@RequestBody SxPackSnReportModel model) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reworkRangeService.queryStandardWorkOrderPackSn(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
