package cn.com.ctop.kuaishou.modules.batch.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.vo.KuaishouReportAccountDailyDTOEntity;
import cn.com.ctop.common.module.vo.SheetInfoVo;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouInfoDTO;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAccountDTO;
import cn.com.ctop.kuaishou.modules.report.entity.ProjectAccountDTO;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyAccountService;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@Slf4j
@RestController
@RequestMapping("/kuaishou")
public class KuaiShouController {

    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouReportDailyAccountService accountService;

    @RequestMapping("report")
    public Map<String, Object> getReport(String accountId, String startDate, String endDate, Integer type, String temporalGranularity) {
        Map<String, Object> result = new HashMap<>();
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
        if (null != type) {
            if (type == 1) {
                //广告主

            } else if (type == 2) {
                //广告组
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getGroupHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getGroupDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            } else if (type == 3) {
                //广告计划
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getCampaignHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getCampaignDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            } else {
                //广告创意
                if (null != temporalGranularity && "hourly".equals(temporalGranularity)) {
                    result.put("data", kuaishouInterfaceService.getCreativeHourlyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                } else {
                    result.put("data", kuaishouInterfaceService.getCreativeDailyReport(token.getAccessToken(), token.getAccountId(), startDate, endDate, 500, 1));
                }
            }
        }
        return result;
    }

    @RequestMapping("/getKuaishouProjectInfo")
    public Result<PageInfo<ProjectAccountDTO>> getKuaishouProjectInfo(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<ProjectAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }
        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<ProjectAccountDTO> projectAccountDTOList = accountService.getKuaishouProjectInfo(dto.getStartDate(), dto.getEndDate(), dto.getProjectList(),dto.getPageSize(),dto.getPageNum(), "", dto.getYn());
            result.setResult(projectAccountDTOList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }finally {
        }
        return result;
    }


    @RequestMapping("/getKuaishouSaleProjectInfo")
    public Result<PageInfo<ProjectAccountDTO>> getKuaishouSaleProjectInfo(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<ProjectAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }
        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<ProjectAccountDTO> projectAccountDTOList = accountService.getKuaishouSaleProjectInfo(dto.getStartDate(), dto.getEndDate(), dto.getProjectList(),dto.getPageSize(),dto.getPageNum(), "",dto.getYn());
            result.setResult(projectAccountDTOList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }

        return result;
    }

    @PostMapping("/exportKuaishouSaleProjectInfo")
    public void exportKuaishouSaleProjectInfo(@RequestBody KuaishouInfoDTO dto, HttpServletResponse response) throws IOException {
        Result<PageInfo<ProjectAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return ;
        }
        //查询总的消耗信息
        SheetInfoVo totalSheetVo = accountService.getKuaishouSaleProjectInfoSheetVO(dto.getStartDate(), dto.getEndDate(), dto.getProjectList(),dto.getPageNum(),dto.getPageSize(),"", dto.getYn());
        OutputStream outputStream = response.getOutputStream();
        String date = DateUtils.formatDate(new Date());
        HttpUtils.setResponseHeader(response, "快手销售报表" + date + ".xlsx");
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);
        Sheet sheet = new Sheet(1, 0, KuaishouReportAccountDailyDTOEntity.class);
        sheet.setSheetName(totalSheetVo.getSheetName());
        excelWriter.write(totalSheetVo.getDetails(), sheet);
        excelWriter.finish();
        outputStream.flush();
        outputStream.close();
    }

    @RequestMapping("/getKuaishouAccountInfoByProjectId")
    public Result<PageInfo<KuaishouReportDailyAccountDTO>> getKuaishouAccountInfoByProjectId(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<KuaishouReportDailyAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }

        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<KuaishouReportDailyAccountDTO> kuaishouReportDailyAccountDTOList = accountService.getKuaishouAccountInfoByProjectId(dto.getProjectId(),dto.getProjectList(), dto.getStartDate(), dto.getEndDate(),dto.getPageSize(),dto.getPageNum(),dto.getYn());
            result.setResult(kuaishouReportDailyAccountDTOList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }finally {
        }

        return result;
    }

    @RequestMapping("/getKuaishouSaleAccountInfoByProjectId")
    public Result<PageInfo<KuaishouReportDailyAccountDTO>> getKuaishouSaleAccountInfoByProjectId(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<KuaishouReportDailyAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }

        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<KuaishouReportDailyAccountDTO> kuaishouReportDailyAccountDTOList = accountService.getKuaishouSaleAccountInfoByProjectId(dto.getProjectList(), dto.getProjectId(), dto.getStartDate(), dto.getEndDate(),dto.getPageSize(),dto.getPageNum(),dto.getYn());
            result.setResult(kuaishouReportDailyAccountDTOList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }finally {
        }

        return result;
    }

    @RequestMapping("/getKuaishouCampaignInfoByAccountId")
    public Result<PageInfo<KuaishouReportDailyAccountDTO>> getKuaishouCampaignInfoByAccountId(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<KuaishouReportDailyAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }

        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<KuaishouReportDailyAccountDTO> kuaishouCampaignInfoList = accountService.getKuaishouCampaignInfoByAccountId(dto.getProjectList(), dto.getAccountId(), dto.getStartDate(), dto.getEndDate(),dto.getPageSize(),dto.getPageNum(),dto.getYn());
            result.setResult(kuaishouCampaignInfoList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }finally {
        }

        return result;
    }

    @RequestMapping("/getKuaishouUnitInfoByCampaignId")
    public Result<PageInfo<KuaishouReportDailyAccountDTO>> getKuaishouUnitInfoByCampaignId(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<KuaishouReportDailyAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }
        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<KuaishouReportDailyAccountDTO> kuaishouCampaignInfoList = accountService.getKuaishouUnitInfoByCampaignId(dto.getProjectList(), dto.getCampaignId(), dto.getStartDate(), dto.getEndDate(),dto.getPageSize(),dto.getPageNum(),dto.getYn());
            result.setResult(kuaishouCampaignInfoList);
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
        }finally {
        }

        return result;
    }

    @RequestMapping("/getKuaishouCreativeInfoByUnitId")
    public Result<PageInfo<KuaishouReportDailyAccountDTO>> getKuaishouCreativeInfoByUnitId(@RequestBody KuaishouInfoDTO dto) {
        Result<PageInfo<KuaishouReportDailyAccountDTO>> result = new Result<>();
        result.setSuccess(true);

        if(dto == null || dto.getStartDate() ==null || dto.getEndDate() == null){
            result.setSuccess(false);
            result.setMessage("参数有误");
            return result;
        }
        if(dto.getPageSize() == 0){
            dto.setPageSize(20);
        }

        try{
            PageInfo<KuaishouReportDailyAccountDTO> kuaishouCampaignInfoList = accountService.getKuaishouCreativeInfoByUnitId(dto.getProjectList(), dto.getUnitId(), dto.getStartDate(), dto.getEndDate(),dto.getPageSize(),dto.getPageNum(),dto.getYn());
            result.setResult(kuaishouCampaignInfoList);
            return result;
        }catch(Exception e){
            log.error(e.getMessage());
            result.setSuccess(false);
            result.setMessage("执行出错");
            return result;
        }

    }
}
