package com.crm.manager.controller;

import com.crm.manager.dto.export.ActivityBaidu202406ExportDto;
import com.crm.manager.service.activity.ActivityBaiduFeignService;
import com.crm.rely.backend.core.dto.activity.baidu.ActivityBaidu202406Dto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.entity.activity.baidu.ActivityBaidu202406ListSearchEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.ExportUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/activity/baidu")
public class ActivityBaiduController {

    @Autowired
    private ActivityBaiduFeignService feignService;

    @PostMapping("/2020406/search/list")
    public BaseResultWithPagerDto<List<ActivityBaidu202406Dto>> searchList(@RequestBody ActivityBaidu202406ListSearchEntity entity) throws ServiceException {
        return feignService.searchPageList(entity);
    }

    @GetMapping("/2020406/export")
    public void tradeCommissionConcludeExport(@RequestParam(name = "name") String name,
                                              @RequestParam(name = "email") String email,
                                              @RequestParam(name = "wechat") String wechat,
                                              @RequestParam(name = "startDate", required = false) String startDate,
                                              @RequestParam(name = "endDate", required = false) String endDate,
                                              HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        ActivityBaidu202406ListSearchEntity entity = new ActivityBaidu202406ListSearchEntity();
        entity.setName(name);
        entity.setEmail(email);
        entity.setWechat(wechat);
        entity.setStartDate(startDate == null ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? null : simpleDateFormat.parse(endDate));
        entity.setPage(null);
        List<ActivityBaidu202406Dto> resultDto =
                feignService.searchList(entity);

            List<ActivityBaidu202406ExportDto> dtos = Lists.newArrayList();
            resultDto.forEach((item) -> {
                ActivityBaidu202406ExportDto dto = new ActivityBaidu202406ExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("ACTIVITYBAIDU"), dtos,
                    ActivityBaidu202406ExportDto.class, response);


    }

    private String genFileName(String fileNamePrefix) {
        StringBuffer stringBuffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        stringBuffer.append(fileNamePrefix);
        stringBuffer.append("_");
        stringBuffer.append(sdf.format(new Date()));
        stringBuffer.append(".xlsx");
        String fileName = stringBuffer.toString();
        return fileName;
    }

}
