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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesContainerSnService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesContainerSnStationService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesContainerSnStation;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Description : 容器条码与站点关系表
 * @Reference :
 * @Author : logic
 * @CreateDate : 2025/6/13 09:15
 * @Modify:
 **/
@RestController
@Api(tags = "容器条码与站点关系表")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mes-container-sn-station")
public class MesContainerSnStationController {

    @Autowired
    private IMesContainerSnStationService mesContainerSnStationService;

    @Autowired
    private IMesContainerSnService mesContainerSnService;

    @GetMapping("/check")
    @ApiOperation(value = "查询")
    public ResultBean queryCheckStation(String organizeCode, String station) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            ValidatorBean.checkNotNull(station, "站点不能为空");
            mesContainerSnStationService.checkStation(organizeCode, station);
            return ResultBean.success("校验成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query")
    @ApiOperation(value = "查询")
    public ResultBean queryPager(MesContainerSnStation mesContainerSnStation, Pager pager) {
        try {
            ValidatorBean.checkNotNull(mesContainerSnStation.getOrganizeCode(), "工厂代码为空");
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setListPager(
                    mesContainerSnStationService.queryPager(mesContainerSnStation, pager));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public ResultBean doInsert(@RequestBody MesContainerSnStation mesContainerSnStation) {
        try {
            ValidatorBean.checkNotNull(mesContainerSnStation.getCreateUser(), "操作人为空");
            ValidatorBean.checkNotNull(mesContainerSnStation.getOrganizeCode(), "工厂代码为空");
            //校验容器条码是否存在
            mesContainerSnService.checkContainerSnIsExist(mesContainerSnStation.getContainerSn(), mesContainerSnStation.getOrganizeCode());
            mesContainerSnStationService.doInsert(mesContainerSnStation, mesContainerSnStation.getCreateUser());
            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete-weakly")
    @ApiOperation(value = "批量软删除信息")
    public ResultBean batchDeleteWeaklyByIds(@RequestBody Long[] idList) {
        try {
            ValidatorBean.checkNotNull(idList, "请选择需要删除的信息");
            List<Long> ids = Arrays.asList(idList);
            String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
            String userName = AuthUtil.getSessionUser().getUserName();
            mesContainerSnStationService.deleteWeaklyByIds(ids, organizeCode, userName);
            return ResultBean.success("批量删除信息成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        }
    }

}
