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.LogUserService;
import com.skr.mdm.bean.dto.UserLogExportDTO;
import com.skr.mdm.bean.qo.log.UserLogPageQO;
import com.skr.mdm.bean.qo.log.UserLogQO;
import com.skr.mdm.bean.vo.system.UserLogVO;
import com.skr.mdm.dao.AuAdminDepartmentDao;
import com.skr.mdm.dao.LogUserDao;
import com.skr.mdm.dao.LogUserOnlineDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.LogUserDO;
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 org.springframework.transaction.annotation.Transactional;

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
@Transactional(rollbackFor = Exception.class)
public class LogUserServiceImpl extends ServiceImpl<LogUserDao, LogUserDO> implements LogUserService {
	@Autowired
	private LogUserDao logUserDao;
	@Autowired
	private AuAdminDepartmentDao adminDepartmentDao;
	@Autowired
	private LogUserOnlineDao logUserOnlineDao;
	@Autowired
	private LogAdminHandler logAdminHandler;

	@Override
	public ResponseResult listUserLog(UserLogPageQO userLogPageQO, LoginAdminDTO admin) {
		UserLogQO userLogQO = new UserLogQO();
		BeanUtils.copyProperties(userLogPageQO, userLogQO);
		userLogQO.setCompanyId(admin.getCompanyId());
		List<Integer> departIdList = adminDepartmentDao.listDepartmentIdByAdminId(admin.getId());
		PageHelper.startPage(userLogPageQO.getPageNum(), userLogPageQO.getPageSize());
		userLogQO.setDepartIdList(departIdList);
		// 上线离线
		if (userLogQO.getModelType() == 1) {
			List<UserLogVO> userLogVOList = logUserOnlineDao.list(userLogQO);
			return ResponseResult.success(PageUtil.getPageResult(userLogVOList));
		}
		return ResponseResult.success(PageUtil.getPageResult(logUserDao.listUserLogVO(userLogQO)));
	}

	@Override
	public void exportUserLog(UserLogQO userLogQO, HttpServletResponse response, LoginAdminDTO admin) throws IOException {
		userLogQO.setCompanyId(admin.getCompanyId());
		List<Integer> departIdList = adminDepartmentDao.listDepartmentIdByAdminId(admin.getId());
		userLogQO.setDepartIdList(departIdList);
		List<UserLogVO> userLogVOList;
		if (userLogQO.getModelType() == 1) {
			userLogVOList = logUserOnlineDao.list(userLogQO);
		} else {
			userLogVOList = logUserDao.listUserLogVO(userLogQO);
		}
		String logType;
		String content = "";
		if (CollectionUtils.isNotEmpty(userLogVOList)) {
			String[] logTypes = new String[]{"上线/离线", "上线/离线", "应用安装", "应用下载", "应用卸载", "发送短信 ", "接受短信 ", "通话呼入", "通话呼出", "签到", "截屏", "拍照", "报警"};
			logType = logTypes[userLogVOList.get(0).getLogType() - 1];
			List<UserLogExportDTO> userLogExportDTOList = userLogVOList.stream().map(userLogVO -> UserLogExportDTO.builder()
					.userName(userLogVO.getUserName())
					.equipmentName(userLogVO.getEquipmentName())
					.departmentName(userLogVO.getDepartmentName())
					.logType(logType)
					.content(userLogVO.getContent())
					.operateTime(userLogVO.getOperateTime())
					.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(), UserLogExportDTO.class)
					.sheet()
					.doWrite(userLogExportDTOList);
		}
		logAdminHandler.log(admin, "", LogAdminTypeEnum.OTHER_TYPE.getLogType(), content, "日志管理");
	}

}