package com.yx.face.service.impl;


import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.dao.AdminDao;
import com.yx.face.dao.UserControlEarlyWarningListLogDao;
import com.yx.face.model.entity.Admin;
import com.yx.face.model.excel.UserEarlyWarningLogExcel;
import com.yx.face.model.query.UserEarlyWarningLogQu;
import com.yx.face.model.vo.UserEarlyWarningLogVO;
import com.yx.face.service.UserControlEarlyWarningListLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 预警日志 服务实现类
 * </p>
 *
 * @author wxg
 * @since 2022-01-24
 */
@Slf4j
@Service
public class UserControlEarlyWarningListLogServiceImpl implements UserControlEarlyWarningListLogService {

    @Resource
    private UserControlEarlyWarningListLogDao userControlEarlyWarningListLogDao;

    @Resource
    private AdminDao adminDao;

    /**
     * @param adminId
     * @return
     */
    @Override
    public List<UserEarlyWarningLogVO> getListLog(Integer adminId) {
        Admin adminInfo = adminDao.selectByPrimaryKey(adminId);
        if (adminInfo == null) {
            throw new ServiceException("无此权限");
        }
        return this.userControlEarlyWarningListLogDao.getListLog(adminInfo);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Integer updateEarlyWarningConfirm(Long id) {
        //预警是否缺认1否2是
        Integer status = 2;
        // 预警id //
        return userControlEarlyWarningListLogDao.updateEarlyWarningConfirm(id, status);
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageInfo<UserEarlyWarningLogVO> getListLogPageInfo(RestDTO<UserEarlyWarningLogQu> dto) {
        HashMap<String, Object> map = new HashMap<>(2);
        UserEarlyWarningLogQu data = dto.getData();
        if (data.getStatus() == null) {
            data.setStatus(0);
        }
        Admin adminVo = adminDao.selectByPrimaryKey(data.getAdminId());
        map.put("adminVo", adminVo);
        map.put("qu", data);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<UserEarlyWarningLogVO> listVo = userControlEarlyWarningListLogDao.getListLogPageInfo(map);
        return new PageInfo<>(listVo);
    }

    @Override
    public void getListLogExcel(UserEarlyWarningLogQu dto, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<>(2);
        if (dto.getStatus() == null) {
            dto.setStatus(0);
        }
        Admin adminVo = adminDao.selectByPrimaryKey(dto.getAdminId());
        map.put("adminVo", adminVo);
        map.put("qu", dto);
        List<UserEarlyWarningLogExcel> listVo = userControlEarlyWarningListLogDao.getUserEarlyWarningLogExcel(map);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        try {
            String fileName = URLEncoder.encode("warning_record", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), UserEarlyWarningLogExcel.class).sheet("预警信息").doWrite(listVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(500,"获取数据过程中异常，请重试");
        }
    }
}
