package com.mvc.controller.upgrade;

import com.mvc.interceptor.Debounce;
import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.upgrade.TUpgradePlanModel;
import com.mvc.service.upgrade.TUpgradePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "升级计划接口")
@RequestMapping("/TUpgradePlanAction")
@Controller
public class TUpgradePlanAction {

    @Resource
    private TUpgradePlanService tUpgradePlanService;

    @ApiOperation("查询所有升级计划")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findAllTUpgradePlan() {
        ResultGeekerModel resultModel;
        try {
            List<TUpgradePlanModel> resList = tUpgradePlanService.findAllTUpgradePlan();
            resultModel = new ResultGeekerModel(resList);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("查询所有升级计划")
    @RequestMapping(value = "/findAllTUpgradePlan", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTUpgradePlan(@RequestBody TUpgradePlanModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = tUpgradePlanService.findAllTUpgradePlan(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增升级计划")
    @RequestMapping(value = "/saveTUpgradePlan", method = RequestMethod.POST)
    @ResponseBody
    @Debounce
    public ResultGeekerModel saveTUpgradePlan(@RequestBody TUpgradePlanModel model) {
        ResultGeekerModel resultModel;
        try {
            String msg = tUpgradePlanService.validateDeviceExistenceInPlan(model);
            if (StringUtils.isNotEmpty(msg)) {
                resultModel = new ResultGeekerModel(msg);
            } else {
                TUpgradePlanModel resModel = tUpgradePlanService.saveTUpgradePlan(model);
                resultModel = new ResultGeekerModel(resModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新升级计划")
    @RequestMapping(value = "/updateTUpgradePlan", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateTUpgradePlan(@RequestBody TUpgradePlanModel model) {
        ResultGeekerModel resultModel;
        try {
            TUpgradePlanModel resModel = tUpgradePlanService.updateTUpgradePlan(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除升级计划")
    @RequestMapping(value = "/deleteTUpgradePlan", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteTUpgradePlan(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = tUpgradePlanService.deleteTUpgradePlan(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
