package com.casic.missiles.modular.group.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ErrorResponseData;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.common.constant.Const;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.group.service.IGroupService;
import com.casic.missiles.modular.group.service.IGroupUserService;
import com.casic.missiles.modular.group.service.IGroupWellService;
import com.casic.missiles.modular.system.dto.BusWellInfoDto;
import com.casic.missiles.modular.system.dto.UserDTO;
import com.casic.missiles.modular.system.dto.WellDTO;
import com.casic.missiles.modular.system.model.Group;
import com.casic.missiles.modular.system.model.GroupUser;
import com.casic.missiles.modular.system.model.GroupWell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 告警组管理控制器
 *
 * @author dev
 * @Date 2020-03-20 10:04:59
 */
@Controller
@RequestMapping("/group")
public class GroupController extends BaseController {


    @Autowired
    private IGroupService groupService;

    /**
     * 告警组用户管理
     */
    @Autowired
    private IGroupUserService groupUserService;

    /**
     * 告警组井管理
     */
    @Autowired
    private IGroupWellService groupWellService;

    /**
     * 基础权限service
     */
    @Autowired
    private ICommonPermissionService permissionService;


    /**
     * 获取告警组管理列表
     *
     * @param keywords  检索关键字
     * @param maxLength 列表中井名称及用户名称最大拼接个数
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String keywords, String maxLength) {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        Page<Group> page = new PageFactory<Group>().defaultPage();
        page.setRecords(groupService.selectPageList(page, dataScope, keywords, maxLength));
        return ResponseData.success(super.packForBT(page));
    }


    /**
     * 新增告警组管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Group group) {
        EntityWrapper<Group> groupEntityWrapper = new EntityWrapper<>();
        groupEntityWrapper.eq("GROUP_NAME", group.getGroupName());
        int count = groupService.selectCount(groupEntityWrapper);
        if (count > 0) {
            return ErrorResponseData.error(400, "分组名称已存在");
        }
        if (ToolUtil.isEmpty(group.getDeptid())) {
            group.setDeptid(permissionService.getCurrLoginUser().getDeptId());
            group.setAlarmOpen(Const.ZERO);
        }
        groupService.insert(group);
        return ResponseData.success();
    }

    /**
     * 删除告警组管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String id) {
        //删除组跟人的绑定关系
        EntityWrapper<GroupUser> groupUserEntityWrapper = new EntityWrapper<>();
        groupUserEntityWrapper.eq("GROUP_ID", Long.valueOf(id));
        groupUserService.delete(groupUserEntityWrapper);
        //删除组跟井的绑定关系
        EntityWrapper<GroupWell> groupWellEntityWrapper = new EntityWrapper<>();
        groupWellEntityWrapper.eq("GROUP_ID", Long.valueOf(id));
        groupWellService.delete(groupWellEntityWrapper);
        groupService.deleteById(Long.valueOf(id));
        return ResponseData.success();
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("groupIds") List<String> groupIds) {
        groupService.deleteBatchIds(groupIds);
        return ResponseData.success();
    }

    /**
     * 修改告警组管理
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object update(Group group) {
        groupService.updateById(group);
        return ResponseData.success();
    }

    /**
     * 告警组管理详情
     */
    @RequestMapping(value = "/detail/{groupId}")
    @ResponseBody
    public Object detail(@PathVariable("groupId") String groupId) {
        return ResponseData.success(groupService.selectById(groupId));

    }

    /**
     * 新增组绑定井
     */
    @RequestMapping(value = "/addWells")
    @ResponseBody
    public Object addWells(String id, @RequestParam("wellIds") List<String> wellIds) {
        groupWellService.addWells(id, wellIds);
        return ResponseData.success();
    }

    /**
     * 新增组绑定人
     */
    @RequestMapping(value = "/addUsers")
    @ResponseBody
    public Object addUsers(String id,@RequestParam("userIds") List<String> userIds) {
        groupUserService.addUsers(id, userIds);
        return ResponseData.success();
    }

    /**
     * •组删除绑定井
     */
    @RequestMapping(value = "/delWell")
    @ResponseBody
    public Object delWell(String id, String wellId) {
        EntityWrapper<GroupWell> query = new EntityWrapper<>();
        query.eq("GROUP_ID", id);
        query.eq("WELL_ID", wellId);
        groupWellService.delete(query);
        return ResponseData.success();
    }

    /**
     * •组删除绑定人
     */
    @RequestMapping(value = "/delUser")
    @ResponseBody
    public Object delUser(String id, String userId) {
        EntityWrapper<GroupUser> query = new EntityWrapper<>();
        query.eq("GROUP_ID", id);
        query.eq("USER_ID", userId);
        groupUserService.delete(query);
        return ResponseData.success();
    }


    /**
     * 获取未分组的井列表,分页
     *
     * @param keywords
     * @param wellType
     * @return
     */
    @RequestMapping(value = "/noGroupWelllist")
    @ResponseBody
    public Object noGroupWellList(String keywords, String wellType) {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        Page<BusWellInfoDto> page = new PageFactory<BusWellInfoDto>().defaultPage();
        page.setRecords(groupService.noGroupWellList(page, dataScope, keywords, wellType));
        return ResponseData.success(super.packForBT(page));
    }


    /**
     * •获取组的人列表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/userlist")
    @ResponseBody
    public Object userList(String id) {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        Page<UserDTO> page = new PageFactory<UserDTO>().defaultPage();
        page.setRecords(groupService.userList(page, dataScope, id));
        return ResponseData.success(super.packForBT(page));
    }


    /**
     * •获取组的井列表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/welllist")
    @ResponseBody
    public Object welllist(String id) {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        Page<WellDTO> page = new PageFactory<WellDTO>().defaultPage();
        page.setRecords(groupService.welllist(page, dataScope, id));
        return ResponseData.success(super.packForBT(page));
    }
}
