package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.dto.event.EventTypeAssigneeAddRequest;
import com.ruoyi.web.domain.dto.event.EventTypeAssigneeEditRequest;
import com.ruoyi.web.domain.dto.event.EventTypeAssigneeQueryRequest;
import com.ruoyi.web.domain.vo.PersonInfoVO;
import com.ruoyi.web.domain.vo.event.EventTypeAssigneeVO;
import com.ruoyi.web.service.EventTypeAssigneeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 事件负责人管理
 */
@Api(tags = "事件负责人分配管理")
@RestController
@RequestMapping("/system/event/type/assignee")
public class EventTypeAssigneeController extends BaseController {

    @Autowired
    private EventTypeAssigneeService eventTypeAssigneeService;

    /**
     * 添加事件负责人
     */
    @ApiOperation("添加事件负责人")
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('system:role:admin')")
    public AjaxResult addEventTypeAssignee(@RequestBody EventTypeAssigneeAddRequest eventTypeAssigneeAddRequest) {
        if (eventTypeAssigneeAddRequest == null) {
            return error("请求参数为空");
        }
        boolean b = eventTypeAssigneeService.addEventTypeAssignee(eventTypeAssigneeAddRequest);
        if (b){
            return success("添加成功");
        }else{
            return error("添加失败");
        }
    }

    /**
     * 删除事件负责人
     */
    @ApiOperation("删除事件负责人")
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('system:role:admin')")
    public AjaxResult deleteEventTypeAssignee(@RequestBody Map<String, Integer> paramsMap) {
        Integer personId = paramsMap.get("personId");
        if (ObjectUtil.isEmpty(personId)) {
            return error("请求参数为空");
        }
        boolean b = eventTypeAssigneeService.deleteEventTypeAssignee(personId);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }

    /**
     * 编辑事件负责人
     */
    @ApiOperation("编辑事件负责人(类型,不能为空,若要取消所有,调删除接口)")
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('system:role:admin')")
    public AjaxResult editEventTypeAssignee(@RequestBody EventTypeAssigneeEditRequest eventTypeAssigneeEditRequest) {
        if (eventTypeAssigneeEditRequest == null) {
            return error("请求参数为空");
        }
        eventTypeAssigneeService.editEventTypeAssignee(eventTypeAssigneeEditRequest);
        return AjaxResult.success();
    }

    /**
     * 根据 personId 获取负责人对应的类型列表
     */
    @ApiOperation("根据村民id获取负责人对应的类型列表")
    @GetMapping("/get")
    @PreAuthorize("@ss.hasPermi('system:role:admin')")
    public AjaxResult getEventTypeAssigneeById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        EventTypeAssigneeVO eventTypeAssigneeVO = eventTypeAssigneeService.getEventTypeAssigneeById(id);
        List<Integer> types = eventTypeAssigneeVO.getTypes();
        Collections.sort(types);
        return success(types);
    }

    /**
     * 根据事件类型获取负责人列表(管理员)
     */
    @ApiOperation("根据事件类型获取负责人列表(管理员)")
    @GetMapping("/list/user")
    public AjaxResult getListEventPersonInfoVOByType(@RequestParam Integer type) {
        if (type == null) {
            return error("请求参数为空");
        }
        List<PersonInfoVO> personInfoVOByEventType = eventTypeAssigneeService.getListEventPersonInfoVO(type);
        return AjaxResult.success(personInfoVOByEventType);
    }

    /**
     * 分页查询对应事件类别的负责人列表
     */
    @ApiOperation("分页查询对应事件类别的负责人列表")
    @PostMapping("/list/page")
    @PreAuthorize("@ss.hasPermi('system:role:admin')")
    public AjaxResult getListEventPersonInfoVOByPage(@RequestBody EventTypeAssigneeQueryRequest eventTypeAssigneeQueryRequest) {
        if (eventTypeAssigneeQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<PersonInfoVO> listEventPersonInfoVOByPage = eventTypeAssigneeService.getListEventPersonInfoVOByPage(eventTypeAssigneeQueryRequest);
        return AjaxResult.success(listEventPersonInfoVOByPage);
    }



} 