/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.plan.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;

import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.GroupuserDto;
import com.tofly.pojo.plan.vo.GroupUserAllVo;
import com.tofly.pojo.plan.vo.GroupuserVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Groupuser;

import com.tofly.gpsboot.service.paln.GroupuserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;
import java.util.Map;

/**
 * 巡查分组配置信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/groupuser")
@Api(tags = "巡查分组配置信息")
public class GroupuserController extends BaseController<Groupuser, GroupuserVo> {
    @Autowired
    private GroupuserService service;

    @Autowired
    public GroupuserController(GroupuserService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡查分组配置信息")
    public ResultRespone<Page<GroupuserVo>> getGroupuserPage(Page page, @ModelAttribute GroupuserDto parDto) {
        ResultRespone<Page<GroupuserVo>> rest = new ResultRespone<>();
        try {
            return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Groupuser.class));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询巡查分组配置信息")
    public ResultRespone<Groupuser> getByIdGroupuser(@PathVariable Long id) {
        ResultRespone<Groupuser> rest = new ResultRespone<>();
        try {
            return super.getById(id);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @GetMapping("/getGroupUserIdNameMap")
    @ApiOperation(value = "根据分组获取该分组下的用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组id", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "根据分组获取该分组下的用户信息")
    public ResultRespone<Map<Long, String>> getGroupUserIdNameMap(Long groupId) {
        ResultRespone<Map<Long, String>> rest = new ResultRespone<>();
        try {
            Map<Long, String> groupUserIdNameMap = service.getGroupUserIdNameMap(groupId);
            rest.setResult(groupUserIdNameMap);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @GetMapping("/getGroupUserMap")
    @ApiOperation(value = "获取当前用户下,部门和巡检组信息")
    @ToFlyAppLog(title = "获取当前用户下,部门和巡检组信息")
    @ApiImplicitParam(name = "departmentId", value = "部门ID", required = true, dataType = "Long")
    public ResultRespone<GroupUserAllVo> getGroupUserMap(Long departmentId, String enableFlag) {
        ResultRespone<GroupUserAllVo> rest = new ResultRespone<>();
        try {

            rest.setResult(service.getGroupUserMap(departmentId,enableFlag));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;

    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping(value = "/save")
    @ToFlyAppLog(title = "新增巡查分组配置信息")
    public ResultRespone<GroupUserAllVo> saveGroupUser(@RequestBody(required = false) GroupUserAllVo groupUserAllVo) {
        ResultRespone<GroupUserAllVo> rest = new ResultRespone<>();
        // 数据校验 判空返回
        try {
            if(null != groupUserAllVo && null != groupUserAllVo.getGroupUserVoList()){
                List<Groupuser> parseArray = JSONArray.parseArray(JSON.toJSONString(groupUserAllVo.getGroupUserVoList()), Groupuser.class);
                GroupUserAllVo userAllVo = service.saveUpdateGroupUserInfo(parseArray);
                rest.setResult(userAllVo);
            }

        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }

        return rest;
    }
}
