package com.crm.manager.controller;

import com.crm.manager.dto.export.RiskSymbolExportDto;
import com.crm.manager.entity.excel.RiskSymbolExcelEntity;
import com.crm.manager.service.risk.RiskSymbolFeignService;
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.symbol.RiskSymbolDto;
import com.crm.rely.backend.core.dto.risk.symbol.record.RiskSymbolRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.risk.symbol.RiskSymbolAddEntity;
import com.crm.rely.backend.core.entity.risk.symbol.RiskSymbolDeleteEntity;
import com.crm.rely.backend.core.entity.risk.symbol.RiskSymbolSearchPageEntity;
import com.crm.rely.backend.core.entity.risk.symbol.RiskSymbolUpdateEntity;
import com.crm.rely.backend.core.entity.risk.symbol.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.base.Strings;
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/symbol")
public class RiskSymbolController {

    @Autowired
    private RiskSymbolFeignService service;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated RiskSymbolAddEntity 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("adjustTime") String adjustTime,
                             @RequestParam("source") Integer source) throws Exception {
        RiskSymbolAddEntity entity = new RiskSymbolAddEntity();
        entity.setName(name);
        entity.setPlatform(platform);
        entity.setSource(source);
        entity.setAdjustTime(DateUtil.parseTime(adjustTime));
        List<RiskSymbolRecordAddEntity> data = createRiskSymbolExcel(file);

        entity.setData(data);

        return service.add(entity);
    }

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

        return service.update(entity);
    }

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

        return service.delete(entity);
    }

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

        return service.searchPage(entity);
    }

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

        return service.searchSingle(entity);
    }

    @PostMapping("/add/record/batch")
    public BaseResultDto addRecordBatch(@RequestBody @Validated RiskSymbolRecordAddBatchEntity 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 RiskSymbolRecordAddSingleEntity entity) throws Exception {

        return service.addRecordSingle(entity);
    }

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

        entity.setSymbolId(symbolId);
        List<RiskSymbolRecordAddEntity> data = createRiskSymbolExcel(file);

        entity.setData(data);

        return service.addRecordBatch(entity);
    }

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


        return service.updateRecord(entity);
    }

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


        return service.deleteRecord(entity);
    }

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

        return service.searchRecordPage(entity);
    }

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

        return service.searchRecordSingle(entity);
    }


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

        List<RiskSymbolExportDto> dtos = Lists.newArrayList();
//        RiskSymbolExportDto dto = new RiskSymbolExportDto();
//        dto.setSymbol("USDCHF");
//        dto.setLongSwap(new BigDecimal(-0.595));
//        dto.setShortSwap(new BigDecimal(-4.44));
//        dtos.add(dto);
        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("SYMBOL-TEMPLATE", false), dtos,
                RiskSymbolExportDto.class, response);
    }

    private List<RiskSymbolRecordAddEntity> createRiskSymbolExcel(MultipartFile file) throws IOException {
        List<RiskSymbolExcelEntity> riskSymbolExcelEntities = EasyExcelUti.readExcel(file,
                new RiskSymbolExcelEntity(), 1);
        List<RiskSymbolRecordAddEntity> data = new ArrayList<>();

        for (RiskSymbolExcelEntity riskSymbolExcelEntity : riskSymbolExcelEntities) {
            RiskSymbolRecordAddEntity riskSymbolRecordAddEntity = new RiskSymbolRecordAddEntity();
            riskSymbolRecordAddEntity.setSymbol(riskSymbolExcelEntity.getSymbol());
            riskSymbolRecordAddEntity.setSwapLong(new BigDecimal(riskSymbolExcelEntity.getLongSwap()));
            riskSymbolRecordAddEntity.setSwapShort(new BigDecimal(riskSymbolExcelEntity.getShortSwap()));
            if (!Strings.isNullOrEmpty(riskSymbolExcelEntity.getCoefficient())) {
                riskSymbolRecordAddEntity.setCoefficient(Integer.parseInt(riskSymbolExcelEntity.getCoefficient()));
            }

            data.add(riskSymbolRecordAddEntity);
        }
        return data;
    }
}
