package com.mvc.controller.app;

import com.mvc.entity.CustomUser;
import com.mvc.entity.FacUser;
import com.mvc.model.*;
import com.mvc.service.app.AppDeviceGroupService;
import com.mvc.service.dic.FacUserService;
import com.mvc.service.taizhang.CustomUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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("/AppDeviceGroupAction")
@Controller
public class AppDeviceGroupAction {

    @Resource
    private AppDeviceGroupService appDeviceGroupService;

    @Resource
    private FacUserService facUserService;

    @Resource
    private CustomUserService customizationService;

    @ApiOperation("根据UserId查询APP设备分组")
    @RequestMapping(value = "/list/{userid}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllAppDeviceGroupByUserid(@PathVariable("userid") String userid) {
        try {
            List<AppDeviceGroupModel> res = appDeviceGroupService.findAllAppDeviceGroupByUserid(userid);
            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("查询APP设备分组")
    @RequestMapping(value = "/findAllAppDeviceGroup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllAppDeviceGroup(@RequestBody AppDeviceGroupModel model) {
        try {
            Map<String, Object> map = appDeviceGroupService.findAllAppDeviceGroup(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 = "/saveAppDeviceGroup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveAppDeviceGroup(@RequestBody AppDeviceGroupModel model) {
        try {
            AppDeviceGroupModel checkModel = appDeviceGroupService.findAppDeviceGroupModelByGroupName(model.getGroupName(), model.getUserId());
            if (checkModel != null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", "收藏列表已经存在[" + model.getGroupName() + "]");
                return resMap;
            }

            AppDeviceGroupModel resModel = appDeviceGroupService.saveAppDeviceGroup(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 = "/updateAppDeviceGroup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody AppDeviceGroupModel model) {
        try {
            if (model.getIsTop() == null || model.getIsTop() != 1) {
                AppDeviceGroupModel checkModel = appDeviceGroupService.findAppDeviceGroupModelByGroupName(model.getGroupName(), model.getUserId());
                if (checkModel != null && checkModel.getId() != model.getId()) {
                    Map<String, Object> resMap = new ErrorMap();
                    resMap.put("error", "收藏列表已经存在[" + model.getGroupName() + "]");
                    return resMap;
                }
            }

            AppDeviceGroupModel resModel = appDeviceGroupService.updateAppDeviceGroup(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 = "/deleteAppDeviceGroup/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteAppDeviceGroup(@PathVariable("id") int id) {
        try {
            int res = appDeviceGroupService.deleteAppDeviceGroup(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("根据appUserid查询AppDeviceGroup列表(内嵌每个设备最新读数)")
    @RequestMapping(value = "/findLatestAiReadByAppUserId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findLatestAiReadByAppUserId(@RequestBody AppDeviceGroupModel model) {
        try {
            Map<String, Object> res = appDeviceGroupService.findLatestAiReadByAppUserId(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;
        }
    }

    @ApiOperation("复制分组")
    @RequestMapping(value = "/copyAppDeviceGroupModel/{groupId}/{appLoginId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> copyAppDeviceGroupModel(@PathVariable("groupId") String groupId, @PathVariable("appLoginId") String appLoginId) {
        try {
            String res = appDeviceGroupService.copyAppDeviceGroupModel(groupId, appLoginId);
            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("复制APP分组到自定义分组(Web端)")
    @RequestMapping(value = "/copyAppDeviceGroupToCustomizeGroup/{id}/{userType}/{loginId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> copyAppDeviceGroupToCustomizeGroup(@PathVariable("id") int id,
                                                                  @PathVariable("userType") String userType,
                                                                  @PathVariable("loginId") String loginId) {
        try {
            FacUser facUser = null;
            CustomUser customUser = null;
            if (userType.equals("0")) {
                facUser = facUserService.findFacUserByLoginId(loginId);
            } else {
                customUser = customizationService.findCustomUserByLoginId(loginId);
            }

            if (facUser == null && customUser == null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", "用户不存在");
                return resMap;
            }

            CustomizeGroupModel res = appDeviceGroupService.copyAppDeviceGroupToCustomizeGroup(id, facUser == null ? customUser : facUser);
            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;
        }
    }
}
