package com.crm.manager.controller;

import com.crm.manager.dto.export.*;
import com.crm.manager.service.CustomLoginHistoryFeignService;
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.user.info.CustomLoginHistoryDto;
import com.crm.rely.backend.core.entity.user.info.CustomHistoryListSearchEntity;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description: 客户登录历史控制器
 * @author: houn
 * @create: 2020-06-13 18:04
 **/
@RestController
@RequestMapping("/custom/login/history")
public class CustomLoginHistoryController {

    @Autowired
    private CustomLoginHistoryFeignService customLoginHistoryFeignService;

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

        return customLoginHistoryFeignService.searcherList(entity);
    }

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

        CustomHistoryListSearchEntity entity = new CustomHistoryListSearchEntity();
        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));
        }

        List<CustomLoginHistoryDto> resultDto = customLoginHistoryFeignService.export(entity);
        List<CustomLoginHistoryExportDto> exportDtos = Lists.newArrayList();
        resultDto.forEach((item) -> {
            CustomLoginHistoryExportDto exportDto = new CustomLoginHistoryExportDto();
            BeanUtils.copyProperties(item, exportDto);
            exportDto.setCID(item.getCId());
            exportDto.setLoginTime(DateUtil.formatTime(item.getLoginTime()));
            exportDtos.add(exportDto);
        });
        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CUSTOM_LOGIN_HISTORY"), exportDtos, CustomLoginHistoryExportDto.class, response);

    }

    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle() {
        return null;
    }
}
