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

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.util.EntityUtil;
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.manage.api.dto.chat.GroupChatDataDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.ChatDataSearchQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatDataApiService;
import com.gic.haoban.manage.web.vo.chat.GroupChatDataVO;
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.apache.commons.collections.CollectionUtils;
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;
import java.util.List;

/**
 * 
* @ClassName: GroupChatDataController
* @Description: 群统计
* @author xugh
* @date 2022年11月26日 上午9:32:56
*
 */
@RestController
@RequestMapping("/chat/data")
public class GroupChatDataController {

    @Autowired
    private GroupChatDataApiService groupChatDataApiService;
    @Autowired
    private GroupChatDataExcelHandler groupChatDataExcelHandler ;

    @RequestMapping("total")
    public RestResponse<Object> listTotalData(@RequestBody ChatDataSearchQDTO qdto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId();
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setStartDate(DateUtil.getStartTimeOfDay(qdto.getStartDate()));
        ServiceResponse<GroupChatDataDTO> resp = this.groupChatDataApiService.listTotalData(qdto);
        GroupChatDataVO vo = EntityUtil.changeEntityByJSON(GroupChatDataVO.class, resp.getResult());
        return RestResponse.successResult(vo);
    }

    @RequestMapping("list-chat")
    public RestResponse<Object> listForChat(@RequestBody ChatDataSearchQDTO qdto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId();
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setStartDate(DateUtil.getStartTimeOfDay(qdto.getStartDate()));
        ServiceResponse<List<GroupChatDataDTO>> resp = this.groupChatDataApiService.listForChat(qdto);
        List<GroupChatDataVO> list = EntityUtil.changeEntityListByJSON(GroupChatDataVO.class, resp.getResult());
        return RestResponse.successResult(list);
    }

    @RequestMapping("list-staff")
    public RestResponse<Object> listStaffData(@RequestBody ChatDataSearchQDTO qdto) {
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qdto.getPageNum());
        basePageInfo.setPageSize(qdto.getPageSize());
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId();
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setStartDate(DateUtil.getStartTimeOfDay(qdto.getStartDate()));
        ServiceResponse<Page<GroupChatDataDTO>> resp = this.groupChatDataApiService.listStaffData(qdto, basePageInfo);
        Page<GroupChatDataDTO> page = resp.getResult();
        List<GroupChatDataVO> list = EntityUtil.changeEntityListByJSON(GroupChatDataVO.class, page.getResult());
        Page<GroupChatDataVO> retPage = new Page<>();
        retPage.setTotalCount(page.getTotalCount());
        retPage.setCurrentPage(page.getCurrentPage());
        retPage.setPageSize(page.getPageSize());
        retPage.setResult(list);
        return RestResponse.successResult(retPage);
    }

    @RequestMapping("excel")
    public RestResponse<String> detailExcel(@RequestBody ChatDataSearchQDTO qdto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(1);
        basePageInfo.setPageSize(1);
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        qdto.setStartDate(DateUtil.getStartTimeOfDay(qdto.getStartDate()));
        ServiceResponse<Page<GroupChatDataDTO>> resp = this.groupChatDataApiService.listStaffData(qdto, basePageInfo);
        List<GroupChatDataDTO> list = resp.getResult().getResult() ;
        if(CollectionUtils.isEmpty(list)) {
            return RestResponse.failure("9999","无客户群数据");
        }
        int totalCount = resp.getResult().getTotalCount();
        String s = DateUtil.formatString(new Date(), DateUtil.FORMAT_DATE_10);
        AuthorizedUser user = new AuthorizedUser();
        user.setUserId(loginUser.getClerkId());
        user.setEnterpriseId(loginUser.getEnterpriseId());
        user.setRealName(loginUser.getClerkName());
        String fileName = "客户群数据-"+s;
        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.QYWX_GROUP_CHAT_DATA);
        task.setUser(user);
        task.setDataType(1);
        task.setTotalCount(totalCount);
        task.setFileName(fileName);
        task.setChannelName(TaskTypeEnum.QYWX_GROUP_CHAT_DATA.getName());
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        task.setSearchDataParams(JSON.toJSONStringWithDateFormat(qdto, DatePattern.NORM_DATETIME_PATTERN));
        task.setHandler(groupChatDataExcelHandler);
        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);
    }

}
