package com.crm.manager.controller;

import com.crm.manager.dto.export.RiskImportTradeExportDto;
import com.crm.manager.entity.excel.RiskImportTradeExcelEntity;
import com.crm.manager.service.risk.RiskImportTradeFeignService;
import com.crm.rely.backend.core.constant.Constants;
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.risk.riskimport.trade.RiskImportTradeDto;
import com.crm.rely.backend.core.dto.risk.riskimport.trade.record.RiskImportTradeRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.risk.riskimport.trade.RiskImportTradeAddEntity;
import com.crm.rely.backend.core.entity.risk.riskimport.trade.RiskImportTradeDeleteEntity;
import com.crm.rely.backend.core.entity.risk.riskimport.trade.RiskImportTradeSearchPageEntity;
import com.crm.rely.backend.core.entity.risk.riskimport.trade.RiskImportTradeUpdateEntity;
import com.crm.rely.backend.core.entity.risk.riskimport.trade.record.*;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.EasyExcelUti;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: houn
 * @create: 2021-03-23 11:06
 **/
@RestController
@RequestMapping("/risk/import/trade")
public class RiskImportTradeController {

    @Autowired
    private RiskImportTradeFeignService service;

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

        return service.add(entity);
    }

    @PostMapping("/add/upload")
    public BaseResultDto add(@RequestParam("file") MultipartFile file,
                             @RequestParam("name") String name,
                             @RequestParam("platform") String platform,
                             @RequestParam("importTime") String importTime,
                             @RequestParam("source") Integer source) throws Exception {
        RiskImportTradeAddEntity entity = new RiskImportTradeAddEntity();
        entity.setName(name);
        entity.setPlatform(platform);
        entity.setSource(source);
        entity.setImportTime(DateUtil.parseTime(importTime));
        List<RiskImportTradeRecordAddEntity> data = createRiskImportTradeExcel(file);

        entity.setData(data);

        return service.add(entity);
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated RiskImportTradeUpdateEntity entity) throws Exception {

        return service.update(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated RiskImportTradeDeleteEntity entity) throws Exception {

        return service.delete(entity);
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<RiskImportTradeDto> searchPage(@RequestBody @Validated RiskImportTradeSearchPageEntity entity) throws Exception {

        return service.searchPage(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto<RiskImportTradeDto> searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.searchSingle(entity);
    }

    @PostMapping("/add/record/batch")
    public BaseResultDto addRecordBatch(@RequestBody @Validated RiskImportTradeRecordAddBatchEntity entity) throws Exception {

        if (entity.getData() == null || entity.getData().size() <= 0) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }

        return service.addRecordBatch(entity);
    }

    @PostMapping("/add/record/single")
    public BaseResultDto addRecordSingle(@RequestBody @Validated RiskImportTradeRecordAddSingleEntity entity) throws Exception {

        return service.addRecordSingle(entity);
    }

    @PostMapping("/add/upload/record")
    public BaseResultDto add(@RequestParam("file") MultipartFile file,
                             @RequestParam("importTradeId") Long importTradeId) throws Exception {
        RiskImportTradeRecordAddBatchEntity entity = new RiskImportTradeRecordAddBatchEntity();

        entity.setImportTradeId(importTradeId);
        List<RiskImportTradeRecordAddEntity> data = createRiskImportTradeExcel(file);

        entity.setData(data);

        return service.addRecordBatch(entity);
    }

    @PostMapping("/update/record")
    public BaseResultDto updateRecord(@RequestBody @Validated RiskImportTradeRecordUpdateEntity entity) throws Exception {


        return service.updateRecord(entity);
    }

    @PostMapping("/delete/record")
    public BaseResultDto deleteRecord(@RequestBody @Validated RiskImportTradeRecordDeleteEntity entity) throws Exception {


        return service.deleteRecord(entity);
    }

    @PostMapping("/search/list/record")
    public ResultWithPagerDto<RiskImportTradeRecordDto> searchRecordPage(@RequestBody @Validated RiskImportTradeRecordSearchPageEntity entity) throws Exception {

        return service.searchRecordPage(entity);
    }

    @PostMapping("/search/single/record")
    public BaseResultDto<RiskImportTradeRecordDto> searchRecordSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.searchRecordSingle(entity);
    }

    @GetMapping("/template/export")
    public void templateExport(HttpServletResponse response) throws Exception {

        List<RiskImportTradeExportDto> dtos = Lists.newArrayList();
        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("IMPORT_TRADE-TEMPLATE", false), dtos, RiskImportTradeExportDto.class, response);
    }

    private List<RiskImportTradeRecordAddEntity> createRiskImportTradeExcel(MultipartFile file) throws IOException {
        List<RiskImportTradeExcelEntity> riskImportTradeExcelEntities = EasyExcelUti.readExcel(file, new RiskImportTradeExcelEntity(), 1);
        List<RiskImportTradeRecordAddEntity> data = new ArrayList<>();

        for (RiskImportTradeExcelEntity riskImportTradeExcelEntity : riskImportTradeExcelEntities) {
            RiskImportTradeRecordAddEntity riskSymbolRecordAddEntity = new RiskImportTradeRecordAddEntity();
            riskSymbolRecordAddEntity.setOrder(Long.parseLong(riskImportTradeExcelEntity.getOrder()));
            riskSymbolRecordAddEntity.setLogin(Long.parseLong(riskImportTradeExcelEntity.getLogin()));
            riskSymbolRecordAddEntity.setSymbol(riskImportTradeExcelEntity.getSymbol());
            if ("buy".equals(riskImportTradeExcelEntity.getCmd()) || "0".equals(riskImportTradeExcelEntity.getCmd())) {
                riskSymbolRecordAddEntity.setCmd(0);
            } else if ("6".equals(riskImportTradeExcelEntity.getCmd()) || "balance".equals(riskImportTradeExcelEntity.getCmd())) {
                riskSymbolRecordAddEntity.setCmd(6);
            } else if ("7".equals(riskImportTradeExcelEntity.getCmd()) || "credit".equals(riskImportTradeExcelEntity.getCmd())) {
                riskSymbolRecordAddEntity.setCmd(7);
            } else {
                riskSymbolRecordAddEntity.setCmd(1);
            }
            riskSymbolRecordAddEntity.setVolume(Integer.parseInt(riskImportTradeExcelEntity.getVolume()));
            riskSymbolRecordAddEntity.setOpenPrice(new BigDecimal(riskImportTradeExcelEntity.getOpenPrice()));
            riskSymbolRecordAddEntity.setOpenTime(DateUtil.parse("yyyy.MM.dd HH:mm:ss",
                    riskImportTradeExcelEntity.getOpenTime()));
            riskSymbolRecordAddEntity.setSl(new BigDecimal(riskImportTradeExcelEntity.getSl()));
            riskSymbolRecordAddEntity.setTp(new BigDecimal(riskImportTradeExcelEntity.getTp()));
            riskSymbolRecordAddEntity.setClosePrice(new BigDecimal(riskImportTradeExcelEntity.getClosePrice()));
            riskSymbolRecordAddEntity.setCloseTime(DateUtil.parse("yyyy.MM.dd HH:mm:ss",
                    riskImportTradeExcelEntity.getCloseTime()));
            riskSymbolRecordAddEntity.setCommission(new BigDecimal(riskImportTradeExcelEntity.getCommission()));
            riskSymbolRecordAddEntity.setProfit(new BigDecimal(riskImportTradeExcelEntity.getProfit()));
            riskSymbolRecordAddEntity.setComment(riskImportTradeExcelEntity.getComment());
            riskSymbolRecordAddEntity.setTaxes(new BigDecimal(riskImportTradeExcelEntity.getTaxes()));
            riskSymbolRecordAddEntity.setStorage(new BigDecimal(riskImportTradeExcelEntity.getStorage()));
            data.add(riskSymbolRecordAddEntity);
        }
        return data;
    }
}
