package com.yunzhi.marketing.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.excel.SalesExport;
import com.yunzhi.marketing.excel.handler.CustomCellWriteHandler;
import com.yunzhi.marketing.service.ITaPersonBuildingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 人员授权项目 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/api")
public class TaPersonBuildingController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaPersonBuildingController.class);

    @Autowired
    private ITaPersonBuildingService taPersonBuildingService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/saleCountList",method= RequestMethod.GET)
    public ResponseBean taShareCountList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "buildingId", required = false) String buildingId,
                                         @RequestParam(value ="startCreateDate",required = false) String startCreateDate,
                                         @RequestParam(value ="endCreateDate",required = false) String endCreateDate, HttpServletRequest request
    ){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = taPersonBuildingService.selectList(pageNum, pageSize, getOrgId(request), buildingId, startCreateDate, endCreateDate, getTaPersonBuildingListByUserId(request));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 销售分享统计导出
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/admin/exportSales")
    public void gethelpRecordExport(@RequestParam(value = "buildingId", required = false) String buildingId,
                                    @RequestParam(value ="startCreateDate",required = false) String startCreateDate,
                                    @RequestParam(value ="endCreateDate",required = false) String endCreateDate,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=销售分享统计.xlsx");
        Integer orgId = getOrgId(request);

        // 设置读取的class
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), SalesExport.class).registerWriteHandler(new CustomCellWriteHandler()).build();
        // 设置 sheet, 同一个sheet只需要设置一次
        WriteSheet writeSheet = EasyExcel.writerSheet("销售分享统计").build();
        List<SalesExport> salesList = taPersonBuildingService.selectSalesList(orgId,buildingId,startCreateDate,endCreateDate,getTaPersonBuildingListByUserId(request));
        excelWriter.write(salesList, writeSheet);
        // finish 会帮忙关闭流
        excelWriter.finish();
    }
}
