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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IProcedureOnSiteService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOnSiteOrder;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOnSiteOrderDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProjectSort;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Api(tags = "过程巡检作业")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN+ "/procedure")
public class ProcedureOnSiteController extends MesPcnBaseController {

    @Autowired
    private IProcedureOnSiteService procedureOnSiteService;

    /**
     * 获取启动状态的工单信息
     *
     * @param workCenterCode
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-work-order/by-wo-status/find")
    @ApiOperation(value = "获取启动状态的工单信息")
    public ResultBean findMesWorkOrderByWoStatus(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultList(procedureOnSiteService.findMesWorkOrderByWoStatus(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 获取创建和点检中状态的巡检单信息
     *
     * @param workOrderNo
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-on-site-order/by-work-order-no/find")
    @ApiOperation(value = "获取创建和点检中状态的巡检单信息")
    public ResultBean findMesOnSiteOrderByWorkOrderNo(String workOrderNo, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultList(procedureOnSiteService.findMesOnSiteOrderByWorkOrderNo(workOrderNo, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 新增巡检单
     *
     * @param mesWorkOrder
     * @return
     */
    @PostMapping(value = "/mes-on-site-order-and-detail/insert")
    @ApiOperation(value = "新增巡检单")
    public ResultBean insertMesOnSiteOrderAndDetail(MesWorkOrder mesWorkOrder, String userName, @RequestParam("ids") List<Long> ids) {
        try {
            procedureOnSiteService.insertMesOnSiteOrderAndDetail(mesWorkOrder, userName, ids);
            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 关闭巡检单
     *
     * @param mesOnSiteOrder
     * @param userName
     * @return
     */
    @PutMapping(value = "/on-site-order/by-status/update")
    @ApiOperation(value = "关闭巡检单")
    public ResultBean updateMesOnSiteOrderByStatus(MesOnSiteOrder mesOnSiteOrder, String userName) {
        try {
            procedureOnSiteService.updateMesOnSiteOrderByStatus(mesOnSiteOrder, userName);
            return ResultBean.success("关闭成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 获取巡检单明细信息
     *
     * @return
     */
    @GetMapping(value = "/procedure-project/find")
    @ApiOperation(value = "获取巡检单明细信息")
    public ResultBean findProcedureProject(MesProjectSort projectSort) {
        try {
            return ResultBean.success("查询成功").setResultList(
                    procedureOnSiteService.findProjectSort(projectSort))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改巡检单明细数据
     *
     * @param mesOnSiteOrderDetail
     * @param userName
     * @return
     */
    @PutMapping(value = "/mes-on-site-order-detail/update")
    @ApiOperation(value = "修改巡检单明细数据")
    public ResultBean updateMesOnSiteOrderDetail(MesOnSiteOrderDetail mesOnSiteOrderDetail, String userName) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(procedureOnSiteService.updateMesOnSiteOrderDetail(mesOnSiteOrderDetail, userName));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 获取巡检单明细信息
     *
     * @param orderNo
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-on-site-order-detail/find")
    @ApiOperation(value = "获取巡检单明细信息")
    public ResultBean findMesOnSiteOrderDetail(String orderNo, String projectSortNo, String checkResult, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultList(
                    procedureOnSiteService.findMesOnSiteOrderDetail(orderNo, projectSortNo, checkResult, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
