package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceDepositExportDto;
import com.crm.manager.service.finance.FinanceDepositFeignService;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
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.deposit.FinanceDepositDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.CustomAmountStatisticsEntity;
import com.crm.rely.backend.core.entity.finance.deposit.*;
import com.crm.rely.backend.util.CountryUtil;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description: 入金
 * @author: houn
 * @create: 2020-07-07 18:01
 **/
@RestController
@RequestMapping("/finance/deposit")
public class FinanceDepositController {

    @Autowired
    private FinanceDepositFeignService financeDepositFeignService;

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

        return financeDepositFeignService.approve(entity);
    }


    @PostMapping("/activity/approve")
    public BaseResultDto activityApprove(@RequestBody @Validated FinanceDepositActivityApproveEntity entity) throws Exception {
        return financeDepositFeignService.activityApprove(entity);
    }

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


    @PostMapping("/supplement")
    public BaseResultDto supplement(@RequestBody @Validated FinanceDepositCallbackUpdateEntity balanceCallbackUpdateEntity) throws Exception {

        return financeDepositFeignService.supplement(balanceCallbackUpdateEntity);
    }

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

        return financeDepositFeignService.delete(entity);
    }

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated FinanceDepositSearchListEntity entity) throws Exception {

        ResultWithPagerDto<FinanceDepositDto> resultWithPagerDto = financeDepositFeignService.searcherList(entity);
        return resultWithPagerDto;
    }

    @GetMapping("/history/export")
    public void tradeHistoryExport(@RequestParam(name = "login") Long login,
                                   @RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "platform") String platform,
                                   @RequestParam(name = "status") Integer status,
                                   @RequestParam(name = "channelCodes") String channelCodes,
                                   @RequestParam(name = "callbackStatus") Integer callbackStatus,
                                   @RequestParam(name = "serial") String serial,
                                   @RequestParam(name = "country") String country,
                                   HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FinanceDepositSearchListEntity entity = new FinanceDepositSearchListEntity();
        entity.setCallbackStatus(callbackStatus);
        entity.setSerial(serial);
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setStatus(status);
        entity.setStartDate(startDate == null ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? null : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        if (Strings.isNullOrEmpty(channelCodes)) {
            entity.setChannelCodes(null);
        } else {

            entity.setChannelCodes(Arrays.asList(channelCodes.split(",")));
        }
        entity.setCountry(country);

        BaseResultDto<List<FinanceDepositDto>> resultDto = financeDepositFeignService.export(entity);
        if (resultDto.getCode() == 200) {
            List<FinanceDepositExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                FinanceDepositExportDto dto = new FinanceDepositExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

//                activity_deposit

                if (item.getActivityDeposit() == null || item.getActivityDeposit().equals(0)) {
                    dto.setActivityDepositName("--");
                } else {
                    if (CountryUtil.isCN(item.getCountry())) {
                        dto.setActivityDepositName("10%");
                    } else {
                        dto.setActivityDepositName("100%");
                    }
                }
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("DEPOSIT"), dtos, FinanceDepositExportDto.class,
                    response);
        }
    }

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

    @RequestMapping("/amount/statictics")
    public BaseResultDto depositAmountStatictics(@RequestBody CustomAmountStatisticsEntity entity) throws Exception {
        return financeDepositFeignService.depositAmountStatictics(entity);
    }

    @RequestMapping("/type/statictics")
    public BaseResultDto depositPayTypeAmountStatictics(@RequestBody CustomAmountStatisticsEntity entity) throws Exception {
        return financeDepositFeignService.depositPayTypeAmountStatictics(entity);
    }

}
