package com.skr.mdm.api.log.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.skr.mdm.api.log.service.LogAdminService;
import com.skr.mdm.bean.dto.AdminLogExportDTO;
import com.skr.mdm.bean.qo.log.AdminLogPageQO;
import com.skr.mdm.bean.qo.log.AdminLogQO;
import com.skr.mdm.bean.vo.admin.AdminVO;
import com.skr.mdm.bean.vo.system.LogAdminVO;
import com.skr.mdm.dao.AuAdminDao;
import com.skr.mdm.dao.AuRoleDao;
import com.skr.mdm.dao.LogAdminDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.LogAdminDO;
import com.skr.mdm.enums.LogAdminTypeEnum;
import com.skr.mdm.handle.LogAdminHandler;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.PageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nfq
 * @date 2023-03-09 09:49
 */
@Service
public class LogAdminServiceImpl extends ServiceImpl<LogAdminDao, LogAdminDO> implements LogAdminService {

    @Autowired
    private LogAdminDao logAdminDao;
    @Autowired
    private AuAdminDao adminDao;
    @Autowired
    private AuRoleDao roleDao;
    @Autowired
    private LogAdminHandler logAdminHandler;

    @Override
    public ResponseResult listAdminLog(AdminLogPageQO adminLogPageQO, LoginAdminDTO loginAdminDTO) {
        AdminLogQO adminLogQO = new AdminLogQO();
        BeanUtils.copyProperties(adminLogPageQO, adminLogQO);
        setAdminIdList(adminLogQO, loginAdminDTO);
        PageHelper.startPage(adminLogPageQO.getPageNum(), adminLogPageQO.getPageSize());
        List<LogAdminVO> logAdminVOList = logAdminDao.listLogAdminVO(adminLogQO);
        return ResponseResult.success(PageUtil.getPageResult(logAdminVOList));
    }

    @Override
    public void exportAdminLog(AdminLogQO adminLogQO, HttpServletResponse response, LoginAdminDTO loginAdminDTO) throws IOException {
        setAdminIdList(adminLogQO, loginAdminDTO);
        List<LogAdminVO> logAdminVOS = logAdminDao.listLogAdminVO(adminLogQO);
        String logType;
        String content = "";
        if (CollectionUtils.isNotEmpty(logAdminVOS)) {
            String[] logTypes = new String[]{"登录", "设备", "用户", "指令", "策略", "应用", "文档", "其他操作", "通信", "报警", "访问控制"};
            logType = logTypes[logAdminVOS.get(0).getLogType() - 1];
            List<AdminLogExportDTO> adminLogExportDTOList = logAdminVOS.stream().map(logAdminVO -> AdminLogExportDTO.builder()
                    .logType(logType)
                    .content(logAdminVO.getContent())
                    .module(logAdminVO.getModule())
                    .operator(logAdminVO.getOperator())
                    .operateUser(logAdminVO.getOperateUser())
                    .createTime(logAdminVO.getCreateTime())
                    .build()).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = "管理员日志_".concat(logType);
            content = fileName.concat("导出");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("content-disposition", fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AdminLogExportDTO.class)
                    .sheet()
                    .doWrite(adminLogExportDTOList);
        }

        logAdminHandler.log(loginAdminDTO, "", LogAdminTypeEnum.OTHER_TYPE.getLogType(), content, "日志管理");

    }

    private void setAdminIdList(AdminLogQO adminLogQO, LoginAdminDTO loginAdminDTO) {
        adminLogQO.setCompanyId(loginAdminDTO.getCompanyId());
        // 获取当前部门
        Integer adminLevel = roleDao.getAdminLevel(loginAdminDTO.getId());
        List<Integer> adminIdList = adminDao.listAdmin(loginAdminDTO.getCompanyId(), null, null, adminLevel).stream().map(AdminVO::getId).collect(Collectors.toList());
        adminIdList.add(loginAdminDTO.getId());
        adminLogQO.setAdminIdList(adminIdList);
    }
}