package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceSynonymTransferExportDto;
import com.crm.manager.entity.excel.FinanceSynonymTransferExcelEntity;
import com.crm.manager.service.finance.FinanceSynonymTransferFeignService;
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.finance.synonym.transfer.FinanceSynonymTransferSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.finance.synonym.transfer.FinanceSynonymTransferAddEntity;
import com.crm.rely.backend.core.entity.finance.synonym.transfer.FinanceSynonymTransferApproveEntity;
import com.crm.rely.backend.core.entity.finance.synonym.transfer.FinanceSynonymTransferDeleteEntity;
import com.crm.rely.backend.core.entity.finance.synonym.transfer.FinanceSynonymTransferSearchEntity;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 异名内转
 * @author: houn
 * @create: 2021-07-12 18:01
 **/
@RestController
@RequestMapping("/finance/synonym/transfer")
public class FinanceSynonymTransferController {

    @Autowired
    private FinanceSynonymTransferFeignService financeSynonymTransferFeignService;


    @PostMapping("/excel")
    public BaseResultDto excel(@RequestParam("file") MultipartFile file) throws Exception {

        List<FinanceSynonymTransferExcelEntity> financeSynonymTransferExcelEntities = EasyExcelUti.readExcel(file,
                new FinanceSynonymTransferExcelEntity(),
                1, FinanceSynonymTransferExcelEntity.class);
        if (financeSynonymTransferExcelEntities == null || financeSynonymTransferExcelEntities.size() <= 0) {
            return BaseResultDto.error(Constants.INFO_NOT_FOUND);
        }
        List<FinanceSynonymTransferAddEntity> entities = new ArrayList<>(financeSynonymTransferExcelEntities.size());
        for (FinanceSynonymTransferExcelEntity financeSynonymTransferExcelEntity :
                financeSynonymTransferExcelEntities) {
            FinanceSynonymTransferAddEntity entity = new FinanceSynonymTransferAddEntity();
            entity.setAmount(new BigDecimal(financeSynonymTransferExcelEntity.getAmount()));
            entity.setWithdrawLogin(Long.parseLong(financeSynonymTransferExcelEntity.getWithdrawLogin()));
            entity.setDepositLogin(Long.parseLong(financeSynonymTransferExcelEntity.getDepositLogin()));
            entities.add(entity);
        }

        return financeSynonymTransferFeignService.saves(entities);
    }

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

        return financeSynonymTransferFeignService.save(entity);
    }

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated FinanceSynonymTransferApproveEntity entity) throws Exception {

        return financeSynonymTransferFeignService.approve(entity);
    }


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

        return financeSynonymTransferFeignService.delete(entity);
    }

    @PostMapping("/searcher/list")
    public ResultWithPagerDto<FinanceSynonymTransferSearchDto> searcherList(@RequestBody @Validated FinanceSynonymTransferSearchEntity entity) throws Exception {

        ResultWithPagerDto<FinanceSynonymTransferSearchDto> resultWithPagerDto =
                financeSynonymTransferFeignService.searcherList(entity);
        return resultWithPagerDto;
    }

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

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

        List<FinanceSynonymTransferExportDto> dtos = Lists.newArrayList();

        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("TEMPLATE", false), dtos,
                FinanceSynonymTransferExportDto.class, response);
    }
}
