package com.casic.missiles.controller.meter;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.meter.MeterCertificateDto;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.meter.MeterCertificateImport;
import com.casic.missiles.dto.meter.MeterStaffRequest;
import com.casic.missiles.service.listeners.CommonExcelListener;
import com.casic.missiles.model.meter.MeterCertificate;
import com.casic.missiles.model.workbench.WorkbenchRemindMessage;
import com.casic.missiles.service.meter.MeterCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "计量人员证书相关接口")
@Slf4j
@RestController
@RequestMapping("/certificate")
public class MeterCertificateController {

    @Resource
    private MeterCertificateService meterCertificateService;

    /**
     * 证书列表查询
     * @return
     */
    @ApiOperation("证书列表查询")
    @PostMapping("/queryCertificateList")
    @ResponseBody
    public ReturnDTO<MeterCertificateDto> queryCertificateList(@RequestBody MeterStaffRequest meterStaffRequest) {
        Page<MeterCertificateDto> page = PageFactory.defaultPage();
        return meterCertificateService.queryCertificateList(page,meterStaffRequest);
    }
    /**
     * 证书新增
     * @return
     */
    @ApiOperation("证书新增")
    @PostMapping("/addStaffCertificate")
    @ResponseBody
    public ReturnDTO addStaffCertificate(@RequestBody List<MeterCertificate> list) {
        return meterCertificateService.addStaffCertificate(list);
    }

    /**
     * 证书删除
     */
    @ApiOperation("证书删除")
    @PostMapping("/deleteCertificate")
    @ResponseBody
    public ReturnDTO deleteCertificate(@RequestBody IdDTO idDto) {
        return meterCertificateService.deleteCertificate(idDto);
    }
    /**
     * 证书详细信息
     */
    @ApiOperation("证书详细信息")
    @PostMapping("/queryStaffCertificateInfo")
    @ResponseBody
    public ReturnDTO<MeterCertificate> queryStaffCertificateInfo(@RequestBody IdDTO idDto) {
        return meterCertificateService.queryStaffCertificateInfo(idDto);
    }

    /**
     * 证书信息编辑
     * @return
     */
    @ApiOperation("证书信息编辑")
    @PostMapping("/updateStaffCertificateInfo")
    @ResponseBody
    public ReturnDTO updateStaffCertificateInfo(@RequestBody MeterCertificate meterCertificate) {
        return meterCertificateService.updateStaffCertificateInfo(meterCertificate);
    }

    /**
     * 证书列表导出
     */
    @ApiOperation("证书列表导出")
    @PostMapping("/exportCertificateList")
    @ResponseBody
    public void exportCertificateList(@RequestBody MeterStaffRequest meterStaffRequest, HttpServletResponse response) {
        try {
            meterCertificateService.exportCertificateList(meterStaffRequest,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 消息列表查询
     * @return
     */
    @ApiOperation("消息列表查询")
    @PostMapping("/queryMessageList")
    @ResponseBody
    public ReturnDTO<WorkbenchRemindMessage> queryMessageList() {
        Page<WorkbenchRemindMessage> page = PageFactory.defaultPage();
        Page<WorkbenchRemindMessage> list = meterCertificateService.queryMessageList(page);
        return ReturnUtil.success(list);
    }

    /**
     * 已读消息
     */
    @ApiOperation("已读消息")
    @PostMapping("/readMessage")
    @ResponseBody
    public ReturnDTO readMessage(@RequestBody IdDTO idDto) {
        return meterCertificateService.readMessage(idDto);
    }

    /**
     * 证书过期提醒
     */
    @ApiOperation("证书过期提醒")
    @PostMapping("/expirationReminder")
    @ResponseBody
    public ReturnDTO expirationReminder(@RequestBody IdDTO idDto) {
        return meterCertificateService.expirationReminder(idDto);
    }

    //@Scheduled(cron = "0 0 0 * * ?")
    private void regularExpirationReminder() {
        meterCertificateService.regularExpirationReminder();
    }

    /**
     *
     * @param multipartFile
     * @return
     */
    @ApiOperation("批量导入证书信息")
    @PostMapping(value = "/batchImportCertificate", headers = "content-type=multipart/form-data")
    @ResponseBody
    public ReturnDTO batchImportCertificate(@RequestParam MultipartFile[] multipartFile) {
        try {
            CommonExcelListener<MeterCertificateImport> listener = new CommonExcelListener<>();
            final long start = System.currentTimeMillis();
            for (MultipartFile file : multipartFile) {
                EasyExcel.read(file.getInputStream(), MeterCertificateImport.class, listener)
                        .headRowNumber(1).autoTrim(true).sheet(0).doRead();
            }
            log.info("耗时：秒" + (System.currentTimeMillis() - start) / 1000);
            //读取到的数据
            List<MeterCertificateImport> list = listener.getList();
            return meterCertificateService.dataCheck(list);
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnUtil.failed("数据取失败");
        }
    }

}
