package com.liquidnet.client.admin.web.controller.zhengzai.tools;

import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.utils.poi.ExcelUtil;
import com.liquidnet.client.admin.zhengzai.kylin.dto.OrderExportVo;
import com.liquidnet.client.admin.zhengzai.kylin.service.IExportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tools/export")
public class ExportDataController extends BaseController {


    @Autowired
    private IExportService exportService;

    private String prefix = "zhengzai/financial";

    @GetMapping()
    public String financial() {
        return prefix + "/export";
    }

    /**
     * 导出订单明细
     *
     * @param exportType
     * @param beginTime
     * @param endTime
     * @param showId
     * @return
     */
    @PostMapping("/export/orderInfo")
    @ResponseBody
    public AjaxResult exportOrderInfo(Integer exportType, String beginTime, String endTime, String showId) {
        if (null == exportType) {
            return error("导出类型有误！");
        }
        List<OrderExportVo> list;
        if (exportType == 1) {//按订单时间导出信息
            if (!timeIsNotNull(beginTime, endTime)) {
                return error("按时间类型导出，开始时间和结束时间不能为空！");
            }
            list = exportService.exportOrderByOrderTime(beginTime, endTime);
        } else if (exportType == 2) {//按演出时间导出信息
            if (!timeIsNotNull(beginTime, endTime)) {
                return error("按时间类型导出，开始时间和结束时间不能为空！");
            }
            list = exportService.exportOrderByShowTime(beginTime, endTime);
        } else if (exportType == 3) {//按id导出信息
            if (!StringUtils.isNotBlank(showId)) {
                return error("演出id不能为空！");
            }
            list = exportService.exportOrderByShowId(showId);
        } else {
            return error("查无信息！");
        }
        if (list.size() == 0) {
            return error("查无信息！");
        }
        ExcelUtil<OrderExportVo> util = new ExcelUtil(OrderExportVo.class);
        return util.exportExcel(list, exportType + "");
    }

    private boolean timeIsNotNull(String beginTime, String endTime) {
        return StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime) ? true : false;
    }

    /**
     * 导出会员订单
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @PostMapping("/export/memberOrder")
    @ResponseBody
    public AjaxResult exportMemberOrder(String beginTime, String endTime) {
        if (!timeIsNotNull(beginTime, endTime)) {
            return error("开始时间和结束时间不能为空！");
        }
        List<OrderExportVo> list;
        list = exportService.exportMemberOrder(beginTime, endTime);
        if (list.size() == 0) {
            return error("查无信息");
        }
        ExcelUtil<OrderExportVo> util = new ExcelUtil(OrderExportVo.class);
        return util.exportExcel(list, "会员订单");
    }

    /**
     * 导出商品订单
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @PostMapping("/export/commodityOrder")
    @ResponseBody
    public AjaxResult exportCommodityOrder(String beginTime, String endTime) {
        if (!timeIsNotNull(beginTime, endTime)) {
            return error("开始时间和结束时间不能为空！");
        }
        List<OrderExportVo> list;
        list = exportService.exportCommodityOrder(beginTime, endTime);
        if (list.size() == 0) {
            return error("查无信息");
        }
        ExcelUtil<OrderExportVo> util = new ExcelUtil(OrderExportVo.class);
        return util.exportExcel(list, "商品订单");
    }

}
