package cn.estsh.i3plus.ext.mes.apiservice.controller.busi.bu;

import cn.estsh.i3plus.ext.mes.api.busi.bu.IBuPatchPartConfirmService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.MesPatchPartConfirmModel;
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.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 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;

@Api(tags = "补件物料确认")
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/sx/bu-ext/patch-part-confirm")
public class BuPatchPartConfirmController {

    @Autowired
    private IBuPatchPartConfirmService patchPartConfirmService;

    @GetMapping(value = "/query-ex-offline")
    @ApiOperation(value = "查询异常下线数据")
    public ResultBean getExOffline(String orderNo, Pager pager) {
        try {
            String userName = AuthUtil.getSessionUser().getUserName();
            String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(patchPartConfirmService.getExOffline(orderNo, userName, organizeCode, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/query-before-part-bom")
    @ApiOperation(value = "查询原始子物料")
    public ResultBean getBeforeOartBom(@RequestBody MesPatchPartConfirmModel patchPartConfirmModel) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(patchPartConfirmService.getBeforeOartBom(patchPartConfirmModel));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query-after-part-bom")
    @ApiOperation(value = "查询新子物料")
    public ResultBean getAfterOartBom(String partNo, String itemPartNo, String itemPartName ,Integer isBindKey) {
        try {
            String userName = AuthUtil.getSessionUser().getUserName();
            String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(patchPartConfirmService.getAfterOartBom(partNo, userName, organizeCode,itemPartNo,itemPartName,isBindKey));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert-update-replace-part-bom")
    @ApiOperation(value = "替换原有子物料号")
    public ResultBean insertUpdateReplacePartBom(@RequestBody MesPatchPartConfirmModel patchPartConfirmModel) {
        try {
            /*List<String> snList = patchPartConfirmModel.getSnList();
            String orderNo = patchPartConfirmModel.getOrderNo();
            String beforePartNo = patchPartConfirmModel.getBeforePartNo();
            String afterPartNo = patchPartConfirmModel.getAfterPartNo();
            String afterPartName = patchPartConfirmModel.getAfterPartName();
            String userName = AuthUtil.getSessionUser().getUserName();
            String organizeCode = AuthUtil.getOrganize().getOrganizeCode();*/
            patchPartConfirmModel.setUserName(AuthUtil.getSessionUser().getUserName());
            patchPartConfirmModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            patchPartConfirmService.insertUpdateReplacePartBom(patchPartConfirmModel);
            return ResultBean.success("替换成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
