package com.bonus.system.wechat.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.MapVo;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.basic.dao.SysOrgDao;
import com.bonus.system.dept.dao.ProDeptRoleDao;
import com.bonus.system.evection.service.EvectionService;
import com.bonus.system.wechat.entity.WechatEvectionBean;
import com.bonus.system.wechat.service.WechatEvectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 数据管理->出差报备
 *
 * @author lsun
 */
@RestController
@RequestMapping("/wechatEvection")
@Slf4j
public class WechatEvectionController extends BaseController {

    @Resource(name = "WechatEvectionService")
    private WechatEvectionService wechatevectionService;

    @Resource(name = "ProDeptRoleDao")
    private ProDeptRoleDao proDeptRoleDao;

    @Resource
    private SysOrgDao sysOrgDao;

    /**
     * 获取数据管理出差报备列表(表格)
     */
    @RequiresPermissions("flow:wechatEvection:list")
    @GetMapping("/list")
    @Log(title = "流程管理->数据管理->出差报备->列表查询", businessType = BusinessType.QUERY)
    public TableDataInfo list(WechatEvectionBean bean) {
        try{
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            List<String> roleId = sysUser.getRoleList();
            //管理员与人资角色可以看到所有
            if (roleId != null && !roleId.isEmpty()) {
                if (Collections.frequency(roleId, "1") == 0 && Collections.frequency(roleId, "16") == 0) {
                    // 根据用户id查询是否是管理员或帮忙负责人
                    Long userId = SecurityUtils.getUserId();
                    List<MapVo> orgList = sysOrgDao.getOrgIdList(userId);
                    if (!orgList.isEmpty()) {
                        // 使用 LinkedHashSet 来保证顺序并自动去重
                        Set<String> idsSet = new LinkedHashSet<>();
                        for (MapVo mapVo : orgList) {
                            String id = proDeptRoleDao.getOrgChildById(mapVo.getId());
                            if (id != null && !id.trim().isEmpty()) { // 防止空或仅含空白字符的字符串
                                // 将分割后的数组添加到 Set 中，自动去重
                                Collections.addAll(idsSet, id.split(","));
                            }
                        }
                        // 如果需要转换回 List 类型
                        List<String> uniqueIds = new ArrayList<>(idsSet);
                        bean.setOrgList(uniqueIds);
                    }else{
                        bean.setUserId(userId);
                    }
                }
            }
            startPage();
            return getDataTable(wechatevectionService.getEvectionList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 根据id获取详细信息
     */
    @RequiresPermissions("flow:wechatEvection:list")
    @GetMapping(value = "/{id}")
    @Log(title = "流程管理->数据管理->出差报备->查询单个出差报备", businessType = BusinessType.QUERY)
    public AjaxResult getInfo(@PathVariable String id) {
        try{
            return success(wechatevectionService.selectEvectionById(id));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }
}