package com.crm.manager.controller;

import com.crm.manager.dto.export.AgentBalanceCommissionTransferExportDto;
import com.crm.manager.dto.export.AgentBalanceTransferExportDto;
import com.crm.manager.service.ib.AgentBalanceCommissionTransferService;
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.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.ib.balance.AgentBalanceCommissionTransferDto;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.balance.AgentBalanceCommissionTransferListEntity;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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;

/**
 * 佣金操作controller
 */
@RestController
@RequestMapping("/agent/balance/commission")
public class AgentBalanceCommissionTransferController {

    private final Logger logger = LoggerFactory.getLogger(AgentBalanceCommissionTransferController.class);

    @Autowired
    private AgentBalanceCommissionTransferService agentBalanceTransferService;

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<AgentBalanceCommissionTransferDto>> searchList(@RequestBody AgentBalanceCommissionTransferListEntity entity, InfoEntity infoEntity) throws Exception {
        return agentBalanceTransferService.searchList(entity);
    }

    @GetMapping("/search/list/export")
    public void searcherListExport(@RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "withdrawIbNo") String withdrawIbNo,
                                   @RequestParam(name = "depositIbNo") String depositIbNo,
                                   HttpServletResponse response) throws Exception {

        AgentBalanceCommissionTransferListEntity entity = new AgentBalanceCommissionTransferListEntity();
        entity.setCId(cId);
        entity.setWithdrawIbNo(withdrawIbNo);
        entity.setDepositIbNo(depositIbNo);

        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);

        BaseResultDto<List<AgentBalanceCommissionTransferDto>> resultDto = agentBalanceTransferService.export(entity);
        if (resultDto.getCode() == Constants.SUCCESS_CODE) {
            List<AgentBalanceCommissionTransferDto> dtos = resultDto.getData();
            List<AgentBalanceCommissionTransferExportDto> exportDtos = Lists.newArrayList();
            dtos.forEach((item) -> {
                AgentBalanceCommissionTransferExportDto exportDto = new AgentBalanceCommissionTransferExportDto();
                BeanUtils.copyProperties(item, exportDto);
                exportDto.setCID(item.getCId());
                exportDto.setPIBNO(item.getPIbNo());
                exportDto.setIbNo(item.getWithdrawIbNo());
                exportDto.setName(item.getName());
                exportDto.setWithdrawAmount(item.getWithdrawAmount().floatValue() * -1F);
                exportDto.setDepositIbNo(item.getDepositIbNo());
                exportDto.setDepositAmount(item.getDepositAmount());
                exportDto.setAddTime(item.getAddTime());
                exportDto.setSerial(item.getSerial());
                exportDtos.add(exportDto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("AGENT_BALANCE_COMMISSION_TRANSFER_EXPORT"), exportDtos,
                    AgentBalanceCommissionTransferExportDto.class, response);
        }
    }

}
