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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.MesEarlyWarningServiceImpl;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.mes.bean.MesEarlyWarningKanBan;
import cn.estsh.i3plus.pojo.mes.model.KanBanPictureModel;
import cn.estsh.i3plus.pojo.mes.model.MesEarlyWarningKanBanModel;
import cn.estsh.i3plus.pojo.mes.model.MesKanBanShowModel;
import cn.estsh.impp.framework.boot.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/earlyWarningKanBan")
public class MesEarlyWarningKanBanController {

    @Autowired
    private MesEarlyWarningServiceImpl mesEarlyWarningService;

    /**
     * 获取所有的线末大屏配置
     * @param organizeCode
     * @return
     */
    @GetMapping(path = "/list/{organizeCode}")
    public ResultBean getEarlyWarningList(@PathVariable String organizeCode){
        List<MesEarlyWarningKanBan> list;
        try {
            list = mesEarlyWarningService.getEarlyWarningList(organizeCode);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功").setResultList(list);
    }

    /**
     * 保存配置
     * @param model
     * @return
     */
    @PostMapping(path = "/save")
    public ResultBean saveEarlyWarningList(@RequestBody MesEarlyWarningKanBanModel model){
        try {
            mesEarlyWarningService.saveEarlyWarning(model);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功");
    }

    /**
     * 更新配置
     * @param earlyWarning
     */
    @PostMapping(path = "/update")
    public ResultBean updateEarlyWarning(@RequestBody MesEarlyWarningKanBan earlyWarning){
        try {
            mesEarlyWarningService.updateEarlyWarning(earlyWarning);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("更新成功");
    }

    /**
     * 软删配置
     */
    @GetMapping(path = "/delete/{id}/{organizeCode}/{userName}")
    public ResultBean deleteEarlyWarning(@PathVariable Long id, @PathVariable String organizeCode,@PathVariable String userName){
        try {
            mesEarlyWarningService.deleteEarlyWarning(id,organizeCode,userName);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("删除成功");
    }


    /**
     * 获取看板配置
     */
    @GetMapping(path = "/get/{id}/{organizeCode}")
    public ResultBean getKanbanData(@PathVariable Long id,@PathVariable String organizeCode){
        MesEarlyWarningKanBan kanbanData;
        try {
            kanbanData = mesEarlyWarningService.getKanbanData(id, organizeCode);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功").setResultObject(kanbanData);
    }


    @GetMapping(path = "/getData/{id}/{organizeCode}")
    public ResultBean getData(@PathVariable Long id, @PathVariable String organizeCode) {
        MesKanBanShowModel data = null;
        try {
            data = mesEarlyWarningService.doGetData(id, organizeCode);
        } catch (Exception e) {
            ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功").setResultObject(data);
    }

    @PostMapping(path = "/save/picture")
    public ResultBean savePicture(@RequestBody KanBanPictureModel kanBanPicture){
        try {
            mesEarlyWarningService.savePicture(kanBanPicture);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultBean.success("保存成功");
    }

}
