package com.mvc.controller.taizhang;

import com.mvc.dao.AppDeviceGroupMapper;
import com.mvc.entity.CustomizeGroup;
import com.mvc.model.*;
import com.mvc.othermodel.GroupAndStationTree;
import com.mvc.service.app.AppDeviceGroupService;
import com.mvc.service.app.AppUserService;
import com.mvc.service.taizhang.CustomizeGroupService;
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;
import java.util.Map;

@Api(tags = "自定义分组表接口")
@RequestMapping("/CustomizeGroupAction")
@Controller
public class CustomizeGroupAction {

    @Resource
    private CustomizeGroupService customizeGroupService;

    @Resource
    private AppUserService appUserService;

    @ApiOperation("分页自定义分组表接口")
    @RequestMapping(value = "/list/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllCustomizeGroup(@PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = customizeGroupService.findAllCustomizeGroup(page, rows);
            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 = "/saveCustomizeGroup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveCustomizeGroup(@RequestBody CustomizeGroupModel model) {
        try {
            Map<String, Object> map = customizeGroupService.saveCustomizeGroup(model);
            if (map.get("error") != null) {
                String errorinfo = (String) map.get("error");
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errorinfo);
                return resMap;
            } else {
                CustomizeGroupModel resmodel = (CustomizeGroupModel) map.get("res");
                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("更新自定义分组表接口")
    @RequestMapping(value = "/updateCustomizeGroup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody CustomizeGroupModel model) {
        try {

            String errMsg = customizeGroupService.checkStationCodes(model);
            if (StringUtils.isNotEmpty(errMsg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errMsg);
                return resMap;
            }

            CustomizeGroupModel resModel = customizeGroupService.updateCustomizeGroup(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("删除自定义分组表接口")
    @RequestMapping(value = "/deleteCustomizeGroup/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteCustomizeGroup(@PathVariable("id") int id) {
        try {
            int res = customizeGroupService.deleteCustomizeGroup(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("获取自定义分组树")
    @RequestMapping(value = "/findCustomizeGroupTree", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findCustomizeGroupTree(@RequestBody CustomizeGroupModel model) {
        try {
            List<GroupAndStationTree> res = customizeGroupService.findCustomizeGroupTree(model.getUnitCode(), model.getCreateUserId());
            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("根据组ID删除其中的站代码")
    @RequestMapping(value = "/deleteStationByGroupId/{groupId}/{stationCode}/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteStationByGroupId(@PathVariable("groupId") String groupId, @PathVariable("stationCode") String stationCode
            , @PathVariable("userId") String userId) {
        try {
            Map<String, Object> map = customizeGroupService.deleteStationByGroupId(groupId, stationCode, userId);
            if (map.get("error") != null) {
                String errorinfo = (String) map.get("error");
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errorinfo);
                return resMap;
            } else {
                Map<String, Object> resMap = new CorrectMap();
                resMap.put("res", map.get("res"));
                return resMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据CustomeGroupQueryModel对象更新分组")
    @RequestMapping(value = "/updateCustomizeGroupByCustomeGroupQueryModel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateCustomizeGroupByCustomeGroupQueryModel(@RequestBody CustomeGroupQueryModel model) {
        try {
            var res = customizeGroupService.updateCustomizeGroupByCustomeGroupQueryModel(model);
            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;
        }
    }

    @Resource
    private AppDeviceGroupService appDeviceGroupService;

    @ApiOperation("复制自定义分组到APP分组")
    @RequestMapping(value = "/copyCustomizeGroupToAppGroup/{groupId}/{loginId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> copyCustomizeGroupToAppGroup(@PathVariable("groupId") String groupId,
                                                            @PathVariable("loginId") String loginId) {
        try {
            String errorMsg = null;
            AppUserModel appUserModel = appUserService.getModelByLoginid(loginId);
            if (appUserModel == null) {
                errorMsg = "用户不存在";
            }

            CustomizeGroupModel customizeGroupModel = customizeGroupService.findCustomizeGroupByGroupId(groupId);
            if (customizeGroupModel == null) {
                errorMsg = "分组已不存在";
            } else {
                AppDeviceGroupModel checkModel = appDeviceGroupService.findAppDeviceGroupModelByGroupName(customizeGroupModel.getGroupName(), appUserModel.getUserId());
                if (checkModel != null) {
                    errorMsg = String.format("当前分组名称[{%s}]已存在", customizeGroupModel.getGroupName());
                }
            }
            if (errorMsg != null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errorMsg);
                return resMap;
            }

            AppDeviceGroupModel res = customizeGroupService.copyCustomizeGroupToAppGroup(customizeGroupModel, appUserModel);
            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;
        }
    }
}
