package com.mvc.controller.dic;

import com.mvc.model.DicGeneralModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.service.dic.DicGeneralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "通用字典")
@RequestMapping("/DicGeneralAction")
@Controller
public class DicGeneralAction {

    @Autowired
    private DicGeneralService dicGeneralService;

    @ApiOperation("查询所有数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findAllDicGeneral() {
        ResultGeekerModel resultModel;
        try {
            List<DicGeneralModel> res = dicGeneralService.findAllDicGeneral();
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("根据字典类型字符串获取对象列表")
    @RequestMapping(value = "/findADicGeneralModelByDicTypes", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findADicGeneralModelByDicTypes(String dicTypes) {
        ResultGeekerModel resultModel;
        try {
            List<DicGeneralModel> res = dicGeneralService.findADicGeneralModelByDicTypes(dicTypes);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增通用字典记录")
    @RequestMapping(value = "/saveDicGeneral", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveDicGeneral(@RequestBody DicGeneralModel model) {
        ResultGeekerModel resultModel;
        try {
            DicGeneralModel temp = dicGeneralService.findADicGeneralModelByDicTypeAndKey(model.getDicType(), model.getDicKey());
            if (temp != null) {
                resultModel = new ResultGeekerModel("当前类型,编号已经存在");
                return resultModel;
            }

            DicGeneralModel resModel = dicGeneralService.saveDicGeneral(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新字典记录")
    @RequestMapping(value = "/updateDicGeneral", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateDeviceAcc(@RequestBody DicGeneralModel model) {
        ResultGeekerModel resultModel;
        try {
            DicGeneralModel temp = dicGeneralService.findADicGeneralModelByDicTypeAndKey(model.getDicType(), model.getDicKey());
            if (temp != null && temp.getId() != model.getId()) {
                resultModel = new ResultGeekerModel("当前类型,编号已经存在");
                return resultModel;
            }

            DicGeneralModel resModel = dicGeneralService.updateDicGeneral(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除字典记录")
    @RequestMapping(value = "/deleteDicGeneral", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteDicGeneral(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = dicGeneralService.deleteDicGeneral(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
