package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceTransferExportDto;
import com.crm.manager.dto.export.FinanceWithdrawExportDto;
import com.crm.manager.service.finance.FinanceTransferFeignService;
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.transfer.FinanceTransferSearchDto;
import com.crm.rely.backend.core.dto.finance.withdraw.FinanceWithdrawDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.finance.transfer.FinanceTransferApproveEntity;
import com.crm.rely.backend.core.entity.finance.transfer.FinanceTransferDeleteEntity;
import com.crm.rely.backend.core.entity.finance.transfer.FinanceTransferSearchEntity;
import com.crm.rely.backend.core.entity.finance.withdraw.FinanceAgentWithdrawSearchListEntity;
import com.crm.rely.backend.core.entity.finance.withdraw.FinanceWithdrawSearchListEntity;
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.base.Strings;
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: 2020-07-07 18:01
 **/
@RestController
@RequestMapping("/finance/transfer")
public class FinanceTransferController {

    @Autowired
    private FinanceTransferFeignService financeTransferFeignService;

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

        return financeTransferFeignService.approve(entity);
    }

//    @PostMapping("/approve/manager")
//    public BaseResultDto approveByManager(@RequestBody @Validated FinanceSynonymTransferApproveManagerEntity
//    entity) throws Exception {
//        if (!Constants.MANAGER_COMPLETED_STATUS.equals(entity.getDepositStatus())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
//        entity.setDepositStatus(Constants.MANAGER_COMPLETED_STATUS);
//        entity.setWithdrawStatus(Constants.MANAGER_COMPLETED_STATUS);
//        financeTransferService.approve(entity);
//        return BaseResultDto.success();
//    }

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

        return financeTransferFeignService.delete(entity);
    }

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

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

    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        BaseResultDto<FinanceTransferSearchDto> 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 = "cId") Long cId,
                                   @RequestParam(name = "status") String status,
                                   @RequestParam(name = "fromLogin") Long fromLogin,
                                   @RequestParam(name = "toLogin") Long toLogin,
                                   @RequestParam(name = "ibNo") String ibNo,
                                   HttpServletResponse response) throws Exception {

        FinanceTransferSearchEntity entity = new FinanceTransferSearchEntity();
        entity.setCId(cId);
        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<FinanceTransferSearchDto>> resultDto = financeTransferFeignService.export(entity);
        if (resultDto.getCode() == Constants.SUCCESS_CODE) {
            List<FinanceTransferSearchDto> dtos = resultDto.getData();
            List<FinanceTransferExportDto> exportDtos = Lists.newArrayList();
            dtos.forEach((item) -> {
                FinanceTransferExportDto exportDto = new FinanceTransferExportDto();
                BeanUtils.copyProperties(item, exportDto);
                exportDto.setCID(item.getCId());
                exportDto.setAmount(item.getAmount().multiply(new BigDecimal(-1)));
                exportDto.setIbNo(item.getPIbNo());
                exportDtos.add(exportDto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CUSTOM_TRANSFER_EXPORT"), exportDtos,
                    FinanceTransferExportDto.class, response);
        }
    }

}
