package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomWalletHistoryExportDto;
import com.crm.manager.service.custom.CustomWalletHistoryFeignService;
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.custom.wallet.CustomWalletHistoryDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.wallet.history.CustomWalletHistorySearchEntity;
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.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/custom/wallet/history")
public class CustomWalletHistoryController {

    @Autowired
    private CustomWalletHistoryFeignService service;


    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomWalletHistoryDto> managerSearcherList(@RequestBody @Validated
                                                                          CustomWalletHistorySearchEntity entity) throws Exception {
        ResultWithPagerDto<CustomWalletHistoryDto> resultWithPagerDto = service.managerSearcherList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/search/single")
    public BaseResultDto managerSearcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.managerSearcherSingle(entity);
    }

    @RequestMapping("/export")
    public void export(@RequestParam(name = "cId") Long cId,
                       @RequestParam(name = "email") String email,
                       @RequestParam(name = "source") String source,
                       @RequestParam(name = "startDate") String startDate,
                       @RequestParam(name = "endDate") String endDate,
                       HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomWalletHistorySearchEntity entity = new CustomWalletHistorySearchEntity();

        entity.setCId(cId);
        entity.setEmail(email);
        entity.setSource(source);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

        BaseResultDto<List<CustomWalletHistoryDto>> resultDto = service.export(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomWalletHistoryExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomWalletHistoryExportDto dto = new CustomWalletHistoryExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_WALLET", true), dtos,
                    CustomWalletHistoryExportDto.class, response);
        }
    }

}
