package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceAgentTransferExportDto;
import com.crm.manager.dto.export.FinanceTransferExportDto;
import com.crm.manager.service.finance.FinanceAgentTransferFeignService;
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.agent.transfer.FinanceAgentTransferSearchDto;
import com.crm.rely.backend.core.dto.finance.transfer.FinanceTransferSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.finance.agent.transfer.FinanceAgentTransferApproveEntity;
import com.crm.rely.backend.core.entity.finance.agent.transfer.FinanceAgentTransferDeleteEntity;
import com.crm.rely.backend.core.entity.finance.agent.transfer.FinanceAgentTransferSearchEntity;
import com.crm.rely.backend.core.entity.finance.transfer.FinanceTransferSearchEntity;
import com.crm.rely.backend.util.DateUtil;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description: 内转
 * @author: houn
 * @create: 2021-06-24 18:01
 **/
@RestController
@RequestMapping("/finance/agent/transfer")
public class FinanceAgentTransferController {

    @Autowired
    private FinanceAgentTransferFeignService financeTransferFeignService;

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

        return financeTransferFeignService.approve(entity);
    }


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

        return financeTransferFeignService.delete(entity);
    }

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

        ResultWithPagerDto<FinanceAgentTransferSearchDto> resultWithPagerDto =
                financeTransferFeignService.searcherList(entity);
        return resultWithPagerDto;
    }

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

    @GetMapping("/searcher/list/export")
    public void searcherListExport(@RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "withdrawCId") Long withdrawCId,
                                   @RequestParam(name = "status") String status,
                                   @RequestParam(name = "fromLogin") Long fromLogin,
                                   @RequestParam(name = "toLogin") Long toLogin,
                                   @RequestParam(name = "ibNo") String ibNo,
                                   HttpServletResponse response) throws Exception {

        FinanceAgentTransferSearchEntity entity = new FinanceAgentTransferSearchEntity();
        entity.setWithdrawCId(withdrawCId);
        entity.setStatus(status);
        entity.setFromLogin(fromLogin);
        entity.setToLogin(toLogin);
        entity.setIbNo(ibNo);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        if (StringUtils.isEmpty(startDate)) {
            entity.setStartDate(DateUtil.getMonthStart(new Date()));
        } else {
            entity.setStartDate(sdf.parse(startDate));
        }

        if (StringUtils.isEmpty(endDate)) {
            entity.setEndDate(DateUtil.addDate(new Date(), 1));
        } else {
            entity.setEndDate(sdf.parse(endDate));
        }
        entity.setPage(null);
        if (StringUtils.isEmpty(entity.getStatus())) entity.setStatus(null);


        BaseResultDto<List<FinanceAgentTransferSearchDto>> resultDto = financeTransferFeignService.export(entity);
        if (resultDto.getCode() == Constants.SUCCESS_CODE) {
            List<FinanceAgentTransferSearchDto> dtos = resultDto.getData();
            List<FinanceAgentTransferExportDto> exportDtos = Lists.newArrayList();
            dtos.forEach((item) -> {
                FinanceAgentTransferExportDto exportDto = new FinanceAgentTransferExportDto();
                BeanUtils.copyProperties(item, exportDto);
                exportDto.setWithdrawCID(item.getWithdrawCId());
                exportDto.setDepositCID(item.getDepositCId());
                exportDto.setAmount(item.getAmount().multiply(new BigDecimal(-1)));
                exportDto.setPIBNO(item.getPIbNo());
                exportDtos.add(exportDto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("FINANCE_AGENT_TRANSFER_EXPORT"), exportDtos,
                    FinanceAgentTransferExportDto.class, response);
        }
    }

}
