package com.mvc.controller.app;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.AppDevgroupDetailModel;
import com.mvc.service.app.AppDevgroupDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "APP分组明细类")
@RequestMapping("/AppDevgroupDetailAction")
@Controller
public class AppDevgroupDetailAction {

    @Resource
    private AppDevgroupDetailService appDevgroupDetailService;

    @ApiOperation("查询APP分组明细")
    @RequestMapping(value = "/list/{groupid}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllAppDevgroupDetailByGroupid(@PathVariable("groupid") String groupid) {
        try {
            List<AppDevgroupDetailModel> res = appDevgroupDetailService.findAllAppDevgroupDetailByGroupid(groupid);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("查询分组明细记录-分页")
    @RequestMapping(value = "/findAllStationAccsByCondition", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllStationAccsByCondition(@RequestBody AppDevgroupDetailModel model) {
        try {
            Map<String, Object> map = appDevgroupDetailService.findAllStationAccsByCondition(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", map);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增APP分组明细记录")
    @RequestMapping(value = "/saveAppDevgroupDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveAppDevgroupDetail(@RequestBody AppDevgroupDetailModel model) {
        try {
            String msg = appDevgroupDetailService.checkDataLegitimacy(model);
            if (StringUtils.isNotEmpty(msg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", msg);
                return resMap;
            }
            AppDevgroupDetailModel resModel = appDevgroupDetailService.saveAppDevgroupDetail(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新APP分组明细")
    @RequestMapping(value = "/updateAppDevgroupDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody AppDevgroupDetailModel model) {
        try {
            AppDevgroupDetailModel resModel = appDevgroupDetailService.updateAppDevgroupDetail(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除APP分组明细")
    @RequestMapping(value = "/deleteAppDevgroupDetail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteAppDevgroupDetail(@PathVariable("id") int id) {
        try {
            int res = appDevgroupDetailService.deleteAppDevgroupDetail(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据groupId和stationCodel删除APP分组明细")
    @RequestMapping(value = "/deleteAppDevgroupDetailByGroupIdAndStationCode/{groupId}/{stationCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteAppDevgroupDetailByGroupIdAndStationCode(@PathVariable("groupId") String groupId, @PathVariable("stationCode") String stationCode) {
        try {
            int res = appDevgroupDetailService.deleteAppDevgroupDetailByGroupId(groupId, stationCode);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
