package com.crm.manager.controller;

import com.crm.manager.dto.export.TradeHistoryExportDto;
import com.crm.manager.dto.export.TwCooperateExportDto;
import com.crm.manager.service.TwCooperateDailyService;
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.cooperate.tw.TwCooperateDto;
import com.crm.rely.backend.core.dto.cooperate.tw.TwCooperateSumDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeHistoryDto;
import com.crm.rely.backend.core.entity.base.SingleLoginEntity;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.cooperate.tw.TwCooperateUpdateEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.cooperate.tw.TwCooperateListEntity;
import com.crm.rely.backend.core.entity.ib.trade.TradeHistoryListEntity;
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("/cooperate")
public class TwCooperateController {

    @Autowired
    private TwCooperateDailyService twCooperateDailyService;

    @PostMapping("/tw/search/list")
    public BaseReportResultDto<List<TwCooperateDto>, TwCooperateSumDto> twSearchList(@RequestBody TwCooperateListEntity entity, InfoEntity infoEntity) throws Exception {
        return twCooperateDailyService.twSearchList(entity);
    }

    @PostMapping("/tw/search/single")
    public BaseResultDto<TwCooperateDto> twSearchSingle(@RequestBody SingleLongEntity entity, InfoEntity infoEntity) throws Exception {
        return BaseResultDto.success(twCooperateDailyService.twSearchSingle(entity.getId()));
    }

    @PostMapping("/tw/update")
    public BaseResultDto<Object> twUpdate(@RequestBody TwCooperateUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        twCooperateDailyService.updateById(entity);
        return BaseResultDto.success();
    }

    @GetMapping("/tw/export")
    public void twExport(@RequestParam(name = "ibNo") String ibNo,
                         @RequestParam(name = "summaryType") Integer summaryType,
                         @RequestParam(name = "startDate") String startDate,
                         @RequestParam(name = "endDate") String endDate,
                         InfoEntity infoEntity,
                         HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TwCooperateListEntity entity = new TwCooperateListEntity();
        entity.setIbNo(ibNo);
        entity.setSummaryType(summaryType);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));

        List<TwCooperateExportDto> resultDto = twCooperateDailyService.getExportDto(entity);

        ExportUtil.transferToResponse(ExportUtil.genFileName("TW_COORPERATE"), resultDto, TwCooperateExportDto.class, response);
    }

}
