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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartSap;
import cn.estsh.i3plus.pojo.mes.bean.MesPartType;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description : 物料信息主数据
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/13 15:13
 * @Modify:
 **/
@RestController
@Api(tags = "物料信息")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mes-part")
public class MesPartController {

    @Autowired
    private IMesPartService mesPartService;

    @GetMapping("/query")
    @ApiOperation(value = "查询信息")
    public ResultBean queryMesPart(MesPart mesPart, Pager pager) {
        try {

            ListPager<MesPart> mesPartListPager = mesPartService.queryMesPart(mesPart, pager);
            List<MesPart> partList = mesPartListPager.getObjectList();
            for (MesPart part : partList) {
                MesPartSap partSap = mesPartService.getMesPartSapByPartNo(part.getPartNo(), part.getOrganizeCode());
                if (partSap != null) {
                    part.setUnit(partSap.getUnit());
                }
            }
            mesPartListPager.setObjectList(partList);
            return ResultBean.success("查询成功").setListPager(mesPartListPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
