package com.crm.manager.controller;

import com.crm.manager.dto.export.AccountTradingActivityExportDto;
import com.crm.manager.service.ib.AccountTradingActivityFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.account.trading.AccountTradingActivityDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.entity.account.trading.AccountTradingActivityListEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
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.List;

/**
 * Created by max on 2020/12/15.
 */
@RestController
@RequestMapping("/account/trading/activity")
public class AccountTradingActivityController {

    @Autowired
    private AccountTradingActivityFeignService accountTradingActivityFeignService;

    @PostMapping("/list")
    public BaseResultWithPagerDto<List<AccountTradingActivityDto>> getList(@RequestBody AccountTradingActivityListEntity entity, InfoEntity infoEntity) throws Exception {
        entity.setStamp(infoEntity.getIbInfo().getStamp());
        return accountTradingActivityFeignService.getList(entity);
    }

    @GetMapping("/export")
    public void export(@RequestParam(name = "login") Long login,
                       @RequestParam(name = "ibNo") String ibNo,
                       @RequestParam(name = "ticketTimeStartDate") String ticketTimeStartDate,
                       @RequestParam(name = "ticketTimeEndDate") String ticketTimeEndDate,
                       @RequestParam(name = "depositTimeStartDate") String depositTimeStartDate,
                       @RequestParam(name = "depositTimeEndDate") String depositTimeEndDate, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        AccountTradingActivityListEntity entity = new AccountTradingActivityListEntity();
        entity.setStamp(infoEntity.getIbInfo().getStamp());
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setTicketTimeStartDate(StringUtils.isEmpty(ticketTimeStartDate) ? null : simpleDateFormat.parse(ticketTimeStartDate));
        entity.setTicketTimeEndDate(StringUtils.isEmpty(ticketTimeEndDate) ? null : simpleDateFormat.parse(ticketTimeEndDate));
        entity.setDepositTimeStartDate(StringUtils.isEmpty(depositTimeStartDate) ? null : simpleDateFormat.parse(depositTimeStartDate));
        entity.setDepositTimeEndDate(StringUtils.isEmpty(depositTimeEndDate) ? null : simpleDateFormat.parse(depositTimeEndDate));

        BaseResultDto<List<AccountTradingActivityDto>> resultDto = accountTradingActivityFeignService.export(entity);
        if (resultDto.getCode() == Constants.SUCCESS_CODE) {
            List<AccountTradingActivityExportDto> exportDtos = Lists.newArrayList();
            for (AccountTradingActivityDto dto : resultDto.getData()) {
                AccountTradingActivityExportDto exportDto = new AccountTradingActivityExportDto();
                BeanUtils.copyProperties(dto,exportDto);
                exportDtos.add(exportDto);
            }
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("ACCOUNT_TRADING_ACTIVITY"), exportDtos, AccountTradingActivityExportDto.class, response);
        }


    }



}
