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.goblin.service.IGoblinCommonService;
import com.liquidnet.client.admin.zhengzai.kylin.dto.*;
import com.liquidnet.client.admin.zhengzai.kylin.service.IExportService;
import com.liquidnet.service.goblin.dto.GoblinStoreSearchDto;
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.Arrays;
import java.util.List;

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

    @Autowired
    private IExportService exportService;

    @Autowired
    private IGoblinCommonService goblinCommonService;


    private String prefix = "zhengzai/financial";

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

    /**
     * 店铺搜索（财务导出用）
     */
    @GetMapping("/store/list")
    @ResponseBody
    public AjaxResult storeList(String storeName) {
        List<String> status = Arrays.asList("3", "4", "5");
        List<GoblinStoreSearchDto> list = goblinCommonService.storeSearch(storeName, status);
        return AjaxResult.success(list);
    }

    /**
     * 导出演出订单
     * 演出id / 下单时间 / 演出开始时间 填了的条件全部 AND
     */
    @PostMapping("/export/orderInfo")
    @ResponseBody
    public AjaxResult exportOrderInfo(String beginTime, String endTime, String showBeginTime, String showEndTime, String showId) {
        boolean hasShowId = StringUtils.isNotBlank(showId);
        boolean hasOrderTime = timeIsNotNull(beginTime, endTime);
        boolean hasShowTime = timeIsNotNull(showBeginTime, showEndTime);
        if (!hasShowId && !hasOrderTime && !hasShowTime) {
            return error("请至少填写演出id、下单时间、演出开始时间其中一个条件！");
        }
        if (hasHalfTime(beginTime, endTime)) {
            return error("下单时间请填写完整的开始和结束时间！");
        }
        if (hasHalfTime(showBeginTime, showEndTime)) {
            return error("演出开始时间请填写完整的开始和结束时间！");
        }
        List<OrderDetailedExportVo> list = exportService.exportShowOrder(showId, beginTime, endTime, showBeginTime, showEndTime);
        if (list.size() == 0) {
            return error("查无信息！");
        }
        ExcelUtil<OrderDetailedExportVo> util = new ExcelUtil(OrderDetailedExportVo.class);
        return util.exportExcel(list, "演出订单");
    }

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

    private boolean hasHalfTime(String beginTime, String endTime) {
        return (StringUtils.isNotBlank(beginTime) && StringUtils.isBlank(endTime))
                || (StringUtils.isBlank(beginTime) && StringUtils.isNotBlank(endTime));
    }


    /**
     * 导出会员订单
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @PostMapping("/export/memberOrder")
    @ResponseBody
    public AjaxResult exportMemberOrder(String beginTime, String endTime) {
        if (!timeIsNotNull(beginTime, endTime)) {
            return error("请填写下单时间范围！");
        }
        List<OrderMemberExportVo> list = exportService.exportMemberOrder(beginTime, endTime);
        if (list.size() == 0) {
            return error("查无信息");
        }
        ExcelUtil<OrderMemberExportVo> util = new ExcelUtil(OrderMemberExportVo.class);
        return util.exportExcel(list, "会员订单");
    }

    /**
     * 导出商品订单
     * 店铺 / 下单时间 填了的条件全部 AND
     *
     * @param beginTime
     * @param endTime
     * @param storeId
     * @return
     */
    @PostMapping("/export/commodityOrder")
    @ResponseBody
    public AjaxResult exportCommodityOrder(String beginTime, String endTime, String storeId) {
        boolean hasStoreId = StringUtils.isNotBlank(storeId);
        boolean hasOrderTime = timeIsNotNull(beginTime, endTime);
        if (!hasStoreId && !hasOrderTime) {
            return error("请至少填写店铺或下单时间其中一个条件！");
        }
        if (hasHalfTime(beginTime, endTime)) {
            return error("下单时间请填写完整的开始和结束时间！");
        }
        List<OrderCommodityExportVo> list = exportService.exportCommodityOrder(beginTime, endTime, storeId);
        if (list.size() == 0) {
            return error("查无信息");
        }
        ExcelUtil<OrderCommodityExportVo> util = new ExcelUtil(OrderCommodityExportVo.class);
        return util.exportExcel(list, "商品订单");
    }

}
