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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpSxSyncBusiService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IHappenStationService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadCellDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesHappenStation;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.RepairRequestModel;
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.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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-16 10:18
 */
@RestController
@Api(tags = "EP质量判定作业")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/ep-ext/")
public class EpQualityControlController {
    @Autowired
    private IHappenStationService happenStationService;

    @Autowired
    private IEpSxSyncBusiService sxSyncBusiService;

    @GetMapping("/happen-station/query")
    @ApiOperation(value = "查询")
    public ResultBean queryEquipmentByPager(MesHappenStation happenStation, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(happenStationService.queryByPager(happenStation, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 新增不良明细数据
     *
     * @param mesBadCellDetailList
     * @param serialNumber
     * @param organizeCode
     * @param userName
     * @return
     */
    @PostMapping(value = "/quality-check/do-freeze")
    @ApiOperation(value = "新增不良明细数据")
    public ResultBean insertMesBadCellAndDetail(@RequestBody List<MesBadCellDetail> mesBadCellDetailList,
                                                String serialNumber, String organizeCode, String workCenterCode,
                                                String workCellCode, String processCode, String userName) {

        try {
            RepairRequestModel repairRequestModel = new RepairRequestModel();
            repairRequestModel.setMesDefectRecordList(mesBadCellDetailList);
            repairRequestModel.setSerialNumber(serialNumber);
            repairRequestModel.setOrganizeCode(organizeCode);
            repairRequestModel.setWorkCenterCode(workCenterCode);
            repairRequestModel.setWorkCellCode(workCellCode);
            repairRequestModel.setProcessCode(processCode);
            repairRequestModel.setModifyUser(userName);
            sxSyncBusiService.doRepairOrQualityDecision(repairRequestModel, 1);
            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
