package com.crm.manager.controller;

import com.crm.manager.dto.export.FinanceDepositGiveExportDto;
import com.crm.manager.service.finance.FinanceDepositGiveFeignService;
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.give.FinanceDepositGiveDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.finance.deposit.give.FinanceDepositGiveAddEntity;
import com.crm.rely.backend.core.entity.finance.deposit.give.FinanceDepositGiveSearchListEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
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.List;

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

    @Autowired
    private FinanceDepositGiveFeignService financeDepositGiveFeignService;

    @PostMapping("/add")
    public BaseResultDto managerAdd(@RequestBody @Validated FinanceDepositGiveAddEntity entity) throws Exception {

        return financeDepositGiveFeignService.add(entity);
    }

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

        ResultWithPagerDto<FinanceDepositGiveDto> resultWithPagerDto =
                financeDepositGiveFeignService.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,
                                   HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FinanceDepositGiveSearchListEntity entity = new FinanceDepositGiveSearchListEntity();
        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);

        BaseResultDto<List<FinanceDepositGiveDto>> resultDto = financeDepositGiveFeignService.export(entity);
        if (resultDto.getCode() == 200) {
            List<FinanceDepositGiveExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                FinanceDepositGiveExportDto dto = new FinanceDepositGiveExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(String.valueOf(item.getCId()));
                dto.setPIBNO(item.getPIbNo());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("DEPOSIT_GIVE"), dtos,
                    FinanceDepositGiveExportDto.class, response);
        }
    }

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

}
