package com.gic.haoban.manage.web.controller.open;

import cn.hutool.core.date.DatePattern;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.JSONResponse;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.commons.util.DateUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.web.qo.open.AlarmWordsLogPageQO;
import com.gic.haoban.manage.web.qo.open.AlarmWordsPageQO;
import com.gic.haoban.manage.web.qo.open.AlarmWordsQO;
import com.gic.haoban.task.manage.api.dto.open.*;
import com.gic.haoban.task.manage.api.qdto.open.AlarmWordsLogQDTO;
import com.gic.haoban.task.manage.api.qdto.open.QwOpenAlarmWordsQDTO;
import com.gic.haoban.task.manage.api.service.OpenQwApiService;
import com.gic.qcloud.BucketNameEnum;
import com.gic.web.common.download.DownloadTask;
import com.gic.web.common.download.DownloadTaskUtil;
import com.gic.web.common.download.constants.TaskTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 企微托管-敏感词
 */
@RestController
@RequestMapping("/qw-open")
public class QywxAlarmWordsController {

    @Autowired
    private OpenQwApiService openQwApiService ;
    @Autowired
    private OpenAlarmWordsLogExcelHandler openAlarmWordsLogExcelHandler ;

    /**
     * 敏感词列表
     */
    @RequestMapping("page-alarm-words")
    public RestResponse<Page<OpenQwAlarmWordsDTO>> pageAlarmWords(@RequestBody AlarmWordsPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qo.getPageNum());
        basePageInfo.setPageSize(qo.getPageSize());
        QwOpenAlarmWordsQDTO qdto = EntityUtil.changeEntityByJSON(QwOpenAlarmWordsQDTO.class,qo) ;
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setEnterpriseId(enterpriseId);
        ServiceResponse<Page<OpenQwAlarmWordsDTO>> resp = this.openQwApiService.alarmWordsPage(qdto,basePageInfo) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存敏感词
     * @param qo
     * @return
     */
    @RequestMapping("save-alarm-words")
    public RestResponse<Void> saveAlarmWords(@RequestBody AlarmWordsQO qo) {
        OpenQwAlarmWordsDTO dto = EntityUtil.changeEntityByJSON(OpenQwAlarmWordsDTO.class,qo) ;
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        dto.setCreatorId(loginUser.getClerkId());
        dto.setCreatorName(loginUser.getClerkName());
        ServiceResponse<Void> resp = this.openQwApiService.saveAlarmWords(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult() ;
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @RequestMapping("del-alarm-words")
    public RestResponse<Void> delAlarmWords(Long id) {
        ServiceResponse<Void> resp = this.openQwApiService.delAlarmWords(id) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult() ;
    }

    /**
     * 敏感词监控列表
     */
    @RequestMapping("page-alarm-words-log")
    public RestResponse<Page<OpenQwAlarmLogDTO>> pageAlarmLog(@RequestBody AlarmWordsLogPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qo.getPageNum());
        basePageInfo.setPageSize(qo.getPageSize());
        AlarmWordsLogQDTO qdto = EntityUtil.changeEntityByJSON(AlarmWordsLogQDTO.class,qo) ;
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setEnterpriseId(enterpriseId);
        qdto.setLoginClerkId(loginUser.getClerkId());
        ServiceResponse<Page<OpenQwAlarmLogDTO>> resp = this.openQwApiService.alarmWordsLogPage(qdto,basePageInfo) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 敏感词监控列表导出
     * @param qo
     * @return
     */
    @RequestMapping("alarm-words-log-excel")
    public RestResponse<String> excel(@RequestBody AlarmWordsLogPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        AlarmWordsLogQDTO qdto = EntityUtil.changeEntityByJSON(AlarmWordsLogQDTO.class,qo) ;
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        qdto.setEnterpriseId(loginUser.getEnterpriseId());
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(1);
        basePageInfo.setPageSize(1);
        qdto.setLoginClerkId(loginUser.getClerkId());
        ServiceResponse<Page<OpenQwAlarmLogDTO>> pageResp =this.openQwApiService.alarmWordsLogPage(qdto,basePageInfo) ;
        final int totalCount = pageResp.getResult().getTotalCount() ;
        if (totalCount == 0) {
            return RestResponse.failure("9999", "导出数据条数为0");
        }
        AuthorizedUser user = new AuthorizedUser();
        user.setUserId(loginUser.getClerkId());
        user.setEnterpriseId(loginUser.getEnterpriseId());
        user.setRealName(loginUser.getClerkName());
        String fileName = "敏感词" ;
        Date startDate = qdto.getStartDate();
        Date endDate = qdto.getEndDate() ;
        if(null != startDate && null != endDate) {
            fileName = fileName + "-" + DateUtil.formatString(startDate, "yyyy-MM-dd") + "到" + DateUtil.formatString(endDate, "yyyy-MM-dd");
        }
        // 1.组装数据查询条件
        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.HAOBAN_ALARM_WORDS);
        task.setUser(user);
        task.setDataType(qo.getDataType());
        task.setTotalCount(totalCount);
        task.setFileName(fileName);
        task.setChannelName(TaskTypeEnum.HAOBAN_ALARM_WORDS.getName());
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        task.setSearchDataParams(JSON.toJSONStringWithDateFormat(qdto, DatePattern.NORM_DATETIME_PATTERN));
        task.setHandler(openAlarmWordsLogExcelHandler);
        JSONResponse jsonResponse = DownloadTaskUtil.createDownloadTask(task);
        final String reportId;
        if (jsonResponse.isSuccess()) {
            reportId = (String) jsonResponse.getResult();
        } else {
            return RestResponse.failure("9999",jsonResponse.getErrorMessage());
        }
        return RestResponse.successResult(reportId);
    }

}
