package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceTradeInterestExportDto;
import com.crm.manager.service.finance.FinanceTradeInterestFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.finance.trade.interest.FinanceTradeInterestDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.finance.trade.interest.FinanceTradeInterestAddEntity;
import com.crm.rely.backend.core.entity.finance.trade.interest.FinanceTradeInterestSearchListEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description:
 * @author: houn
 * @create: 2022-04-11 18:01
 **/
@RestController
@RequestMapping("/finance/trade/interest")
public class FinanceTradeInterestController {

    @Autowired
    private FinanceTradeInterestFeignService feignService;

    @PostMapping("/add")
    public BaseResultDto managerAdd(@RequestBody @Validated FinanceTradeInterestAddEntity entity) throws Exception {

        return feignService.add(entity);
    }

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated FinanceTradeInterestSearchListEntity entity) throws Exception {

        ResultWithPagerDto<FinanceTradeInterestDto> resultWithPagerDto = feignService.searcherList(entity);
        return resultWithPagerDto;
    }

    @GetMapping("/history/export")
    public void tradeHistoryExport(@RequestParam(name = "login") Long login,
                                   @RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "platform") String platform,
                                   @RequestParam(name = "status") Integer status,
                                   HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FinanceTradeInterestSearchListEntity entity = new FinanceTradeInterestSearchListEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setStatus(status);
        entity.setStartDate(startDate == null ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? null : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);

        BaseResultDto<List<FinanceTradeInterestDto>> resultDto = feignService.export(entity);
        if (resultDto.getCode() == 200) {
            List<FinanceTradeInterestExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                FinanceTradeInterestExportDto dto = new FinanceTradeInterestExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(String.valueOf(item.getCId()));
                dto.setPIBNO(item.getPIbNo());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("TRADE_INTEREST"), dtos,
                    FinanceTradeInterestExportDto.class, response);
        }
    }

    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        BaseResultDto<FinanceTradeInterestDto> resultDto = feignService.searcherSingle(entity);
        return resultDto;
    }

}
