package com.kiisoo.ic.analysis.controller;

import com.kiisoo.ic.analysis.bean.TableAnalysisDTO;
import com.kiisoo.ic.analysis.service.TableAnalysisService;
import com.kiisoo.ic.common.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @author yechenhao
 * @date 2020/5/9 14:24
 * @company kiisoo
 * @details 报表分析controller
 */
@Controller
@RequestMapping("/analysis")
@Slf4j
public class TableAnalysisController extends BaseController {

    @Autowired
    private TableAnalysisService tableAnalysisService;

    /**
     * 查询
     * @param selectStartTime 开始时间
     * @param selectEndTime 结束时间
     * @return
     */
    @RequestMapping(value = "/add/customer",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listCompanyAddCustomer(@RequestParam("selectStartTime") String selectStartTime,
                                                     @RequestParam("selectEndTime") String selectEndTime){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<TableAnalysisDTO> tableAnalysisDTOS = tableAnalysisService.listCompanyAddCustomer(sdf.parse(selectStartTime), sdf.parse(selectEndTime));
            return data(tableAnalysisDTOS);
        }catch (Exception e){
            log.error("",e);
            return fail();
        }
    }

    /**
     * 查询
     * @param selectStartTime 开始时间
     * @param selectEndTime 结束时间
     * @return
     */
    @RequestMapping(value = "/apply/customer",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listCompanyApplyCustomer(@RequestParam("selectStartTime") String selectStartTime,
                                                     @RequestParam("selectEndTime") String selectEndTime,
                                                       @RequestParam("count")Integer count){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<TableAnalysisDTO> tableAnalysisDTOS = tableAnalysisService.listCompanyApplyCustomer(sdf.parse(selectStartTime), sdf.parse(selectEndTime),count);
            return data(tableAnalysisDTOS);
        }catch (Exception e){
            log.error("",e);
            return fail();
        }
    }

    /**
     * 下载excel
     * @param title 标题
     * @param jsonData 数据
     * @param company 公司名
     * @param response response
     * @return
     */
    @RequestMapping(value = "/download/excel",method = RequestMethod.POST)
    public void downloadExcel(@RequestParam("title") String title,
                                            @RequestParam("jsonData") String jsonData,
                                            @RequestParam("company")String company, HttpServletResponse response){
        try {
            tableAnalysisService.downloadExcel(title,jsonData,company,response);
        }catch (Exception e){
            log.error("下载excel失败",e);
        }
    }
}
