package com.crm.manager.controller;

import com.crm.manager.dto.export.AgentBalanceTransferExportDto;
import com.crm.manager.service.ib.AgentBalanceTransferService;
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.AgentBalanceTransferDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.balance.AgentBalanceTransferApproveEntity;
import com.crm.rely.backend.core.entity.ib.balance.AgentBalanceTransferListEntity;
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.web.bind.annotation.*;

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

/**
 * Created by max on 2020/8/4.
 */
@RestController
@RequestMapping("/agent/balance/transfer")
public class AgentBalanceTransferController {

    @Autowired
    private AgentBalanceTransferService agentBalanceTransferService;

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

    @PostMapping("/search/single")
    public BaseResultDto<AgentBalanceTransferDto> searchSingle(@RequestBody SingleLongEntity entity, InfoEntity infoEntity) throws Exception {

        return BaseResultDto.success(agentBalanceTransferService.searchSingle(entity));
    }

    @PostMapping("/approve")
    public BaseResultDto<Object> approve(@RequestBody AgentBalanceTransferApproveEntity entity, InfoEntity infoEntity) throws Exception {
        return agentBalanceTransferService.approve(entity);
    }


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

        AgentBalanceTransferListEntity entity = new AgentBalanceTransferListEntity();
        entity.setStatus(status);
        entity.setTo(to);
        entity.setCId(cId);
        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<AgentBalanceTransferDto>> resultDto = agentBalanceTransferService.export(entity);
        if (resultDto.getCode() == Constants.SUCCESS_CODE) {
            List<AgentBalanceTransferDto> dtos = resultDto.getData();
            List<AgentBalanceTransferExportDto> exportDtos = Lists.newArrayList();
            dtos.forEach((item) -> {
                AgentBalanceTransferExportDto exportDto = new AgentBalanceTransferExportDto();
                BeanUtils.copyProperties(item, exportDto);
                exportDto.setCID(item.getCId());
                exportDto.setPIBNO(item.getPIbNo());
                exportDto.setIbNo(item.getIbNo());
                exportDto.setWithdrawName(item.getName());
                exportDto.setWithdrawIbNo(item.getIbNo());
                exportDto.setAmount(item.getAmount() * -1F);
                exportDto.setDepositLogin(item.getTo());
                exportDto.setDepositAmount(item.getDepositAmount());
                exportDto.setAddTime(item.getAddTime());
                exportDto.setWithdrawSalesNo(item.getWithdrawSalesNo());
                exportDto.setDepositName(item.getDepositName());
                exportDto.setDepositSalesNo(item.getDepositSalesNo());
                exportDto.setDepositPlatform(item.getPlatform());
                exportDto.setSerial(item.getSerial());
                exportDtos.add(exportDto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("AGENT_BALANCE_TRANSFER_EXPORT"), exportDtos,
                    AgentBalanceTransferExportDto.class, response);
        }
    }

}
