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

import cn.estsh.i3plus.ext.mes.api.base.IPlantInfoService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPlantInfo;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
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 cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/03
 * @Description:
 **/
@RestController
@Api(tags = "工厂信息")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/plant-info-data")
@Slf4j
public class PlantInfoController {
    public static final Logger LOGGER = LoggerFactory.getLogger(PlantInfoController.class);

    @Autowired
    private IPlantInfoService plantInfoService;

    @PutMapping(value = "/update")
    @ApiOperation(value = "更新工厂信息", notes = "更新工厂信息")
    public ResultBean updatePlantInfo(@RequestBody MesPlantInfo plantInfo) {
        try {
            log.info("更新工厂信息plantInfo:{}", plantInfo);
            //条件验证
            ValidatorBean.beginValid(plantInfo)
                    .notNull("plantName", plantInfo.getPlantName())
                    .notNull("crewSize", plantInfo.getCrewSize())
                    //.notNull("onenessWashlet", plantInfo.getOnenessWashlet())
                    //.notNull("dishWashingMachine", plantInfo.getDishWashingMachine())
                    //.notNull("bidet", plantInfo.getBidet())
                    .notNull("content", plantInfo.getContent());

            plantInfo.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            plantInfoService.updatePlantInfo(plantInfo);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    //@PutMapping(value = "/bu/update")
    //@ApiOperation(value = "更新BU工厂信息", notes = "更新BU工厂信息")
    //public ResultBean updateBuPlantInfo(@RequestBody MesPlantInfo plantInfo) {
    //    try {
    //        log.info("更新BU工厂信息plantInfo:{}", plantInfo);
    //        //条件验证
    //        ValidatorBean.beginValid(plantInfo)
    //                .notNull("plantName", plantInfo.getPlantName())
    //                .notNull("crewSize", plantInfo.getCrewSize())
    //                .notNull("content", plantInfo.getContent());
    //
    //        plantInfo.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
    //        plantInfoService.updatePlantInfo(plantInfo);
    //        return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    //    } catch (ImppBusiException busExcep) {
    //        return ResultBean.fail(busExcep);
    //    } catch (Exception e) {
    //        return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
    //    }
    //}

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询工厂信息", notes = "按条件分页查询工厂信息")
    public ResultBean queryPlantInfoByPager(String organizeCode) {
        try {
            // plantInfo.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setResultList(plantInfoService.queryPlantInfo(organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
