package com.bonus.system.basic.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.SysRole;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.basic.dao.SysUserMapper;
import com.bonus.system.basic.domain.SysNotice;
import com.bonus.system.basic.service.SysNoticeService;
import com.bonus.system.holiday.dao.WorkReportDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @description 我的消息
* @author hay
* @date   2024/10/17 13:18
*/
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private SysNoticeService noticeService;

    @Resource(name = "WorkReportDao")
    private WorkReportDao dao;

    @Resource
    private SysUserMapper userMapper;

    @RequiresPermissions("system:notice:list")
    @GetMapping("/list")
    @Log(title = "系统管理->我的消息->查询消息列表", businessType = BusinessType.QUERY)
    public TableDataInfo list(SysNotice bean) {
        try{
            Long userId = SecurityUtils.getUserId();
            List<SysRole> sysRoleList = dao.getRoleListByUserId(userId);
            int roleCount = 0;
            if(!sysRoleList.isEmpty()){
                for (SysRole sysRole : sysRoleList) {
                    if(sysRole.getRoleName().contains("管理员")){
                        roleCount = 1;
                        break;
                    }else if(sysRole.getRoleName().contains("人资专员")){
                        roleCount = 1;
                        break;
                    } else if(sysRole.getRoleName().contains("部门考勤员")){
                        roleCount = 2;
                        break;
                    }else if(sysRole.getRoleName().contains("考勤员")){
                        roleCount = 2;
                        break;
                    }else if(sysRole.getRoleName().contains("部门负责人")){
                        roleCount = 2;
                        break;
                    }
                }
            }
            if(roleCount==2){
                String ids = userMapper.getOrg(userId);
                bean.setRoleType("2");
                String[] split = ids.split(",");
                bean.setOrgListId(split);
                //查询是否存在多个部门负责人，
                List<SysRole> managerList = userMapper.getManagerList(userId);

                if( managerList !=null &&  managerList.size()>0){
                    bean.setUserIdBr(userId);
                }
            }else if(roleCount==1){
                bean.setRoleType("1");
            } else{
                bean.setRoleType("3");
                bean.setUserId(userId);
            }
            List<SysNotice> list = noticeService.selectNoticeList(bean);
            return endPage(list);
        }catch (Exception e){
            logger.error(e.toString(),e);
        }
        return  getDataTableError(new ArrayList<>());
    }

    /**
     * 修改状态
     */
    @Log(title = "系统管理->我的消息->修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("updateStatus")
    public AjaxResult edit(@Validated @RequestBody SysNotice bean) {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        bean.setUserId(userId);
        return noticeService.updateStatus(bean);
    }

    /**
     * 消息新增
     */
    @PostMapping("/addNotice")
    @Log(title = "系统管理->我的消息->消息新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody SysNotice bean) {
        return noticeService.insertNotice(bean);
    }
}
