package com.crm.manager.controller;


import com.crm.manager.dto.export.TradeHistoryExportDto;
import com.crm.manager.dto.export.TradeHistoryExportEnDto;
import com.crm.manager.service.ib.TradeShardingFeignService;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeHistoryDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeHistorySumDto;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.trade.TradeHistoryListEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * Created by max on 2020/12/2.
 */
@RestController
@RequestMapping("/trade/sharding")
public class TradeReportShardingController {

    @Autowired
    private TradeShardingFeignService tradeShardingFeignService;

    /**
     * 交易历史
     *
     * @param entity
     * @param infoEntity
     * @throws Exception
     */
    @PostMapping("/history")
    public BaseReportResultDto<List<TradeHistoryDto>, TradeHistorySumDto> tradeHistory(@RequestBody TradeHistoryListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeShardingFeignService.tradeHistory(entity);
    }

    @GetMapping("/history/export")
    public void tradeHistoryExport(@RequestParam(name = "login") Long login,
                                   @RequestParam(name = "ibNo") String ibNo,
                                   @RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "platform") String platform,
                                   @RequestParam(name = "groupType") Long groupType,
                                   @RequestParam(name = "agentId") Long agentId,
                                   @RequestParam(name = "lang") String lang,
                                   @RequestParam(name = "isShort") Integer isShort,
                                   @RequestParam(name = "loginTypes") List<Integer> loginTypes,
                                   InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeHistoryListEntity entity = new TradeHistoryListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setGroupType(groupType);
        entity.setAgentId(agentId);
        entity.setIsShort(isShort);
        entity.setLoginTypes(loginTypes);

        BaseResultDto<List<TradeHistoryDto>> resultDto = tradeShardingFeignService.tradeHistoryExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeHistoryExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeHistoryExportDto dto = new TradeHistoryExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("HISTORY"), dtos, TradeHistoryExportDto.class, response);
            }else {
                List<TradeHistoryExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeHistoryExportEnDto dto = new TradeHistoryExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("HISTORY"), dtos, TradeHistoryExportEnDto.class, response);
            }

        }
    }

}
