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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISpeParamNewService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISpeParamService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSpeParam;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSpeParamResult;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/4 18:05
 **/
@RestController
@Api(tags = "特殊工艺参数")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/spe-param")
public class SpeParamController {
    @Autowired
    private ISpeParamService speParamService;

    @Autowired
    private ISpeParamNewService speParamNewService;

    @GetMapping("query")
    @ApiOperation(value = "条件分页")
    public ResultBean queryPager(MesSpeParam detail) {
        try {
            List<MesSpeParam> mesSpeParamList = this.speParamService.queryList(detail);
            MesSpeParamResult paramResultById = new MesSpeParamResult();
            if (!CollectionUtils.isEmpty(mesSpeParamList)) {
                MesSpeParam speParam = mesSpeParamList.get(0);
                if (speParam.getSpeParamResultId() != null && speParam.getSpeParamResultId() != 0) {
                    paramResultById = speParamNewService.getParamResultById(speParam.getSpeParamResultId());
                    for (MesSpeParam mesSpeParam : mesSpeParamList) {
                        mesSpeParam.setResult1(paramResultById.getResult1());
                        mesSpeParam.setResult2(paramResultById.getResult2());
                        mesSpeParam.setResult3(paramResultById.getResult3());
                        mesSpeParam.setResult4(paramResultById.getResult4());
                        mesSpeParam.setResult5(paramResultById.getResult5());
                        mesSpeParam.setResult6(paramResultById.getResult6());
                        mesSpeParam.setResult7(paramResultById.getResult7());
                        mesSpeParam.setResult8(paramResultById.getResult8());
                        mesSpeParam.setResult9(paramResultById.getResult9());
                        mesSpeParam.setResult10(paramResultById.getResult10());
                    }
                }

            }
            return ResultBean.success("查询成功").setResultList(mesSpeParamList).setResultObject(paramResultById)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping("query-equ")
    @ApiOperation(value = "查询设备")
    public ResultBean queryParamNo(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功")
                    .setResultList(speParamNewService.queryEquipment(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("refresh")
    @ApiOperation(value = "刷新")
    public ResultBean refresh(String workCenterCode, String equNo, String equName, String organizeCode) {
        try {
            speParamNewService.insertSpeParams(workCenterCode, equNo, equName, organizeCode);
            return ResultBean.success("刷新成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("save")
    @ApiOperation(value = "保存")
    public ResultBean updateSpeParams(@RequestBody List<MesSpeParam> speParams) {
        try {
            speParamNewService.updateSpeParams(speParams);
            return ResultBean.success("保存成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
