package com.huiju.estateagents.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.DateUtils;
import com.huiju.estateagents.common.ExcelUtils;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.excel.ActivityDailyExport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.huiju.estateagents.service.ITsActivityDailyService;
import com.huiju.estateagents.entity.TsActivityDaily;

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

/**
 * <p>
    * 活动统计  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/api/admin/stats")
public class TsActivityDailyController extends BaseController {

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

    @Autowired
    public ITsActivityDailyService iTsActivityDailyService;

    @GetMapping("/activity/detail")
    public ResponseBean getActivityDetail(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                          // startDate 默认格式 YYYY-MM-DD
                                          @RequestParam(value ="startDate", required = false) String startDate,
                                          @RequestParam(value ="endDate", required = false) String endDate,
                                          @RequestParam(value ="buildingId", required = false) String buildingId,
                                          @RequestParam(value ="targetType", required = false) String targetType,
                                          @RequestParam(value ="targetId", required = false) String targetId,
                                          @RequestParam(value ="targetName", required = false) String targetName,
                                          @RequestParam(value ="asc", required = false) String asc,
                                          @RequestParam(value ="desc", required = false) String desc,
                                          HttpServletRequest request) {

        if (null == startDate || "".equals(startDate)) {
            return ResponseBean.error("开始日期不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }
        if (null == endDate || "".equals(endDate)) {
            endDate = DateUtils.today();
        }

        Integer orgId = getOrgId(request);

//        IPage<TsActivityDaily> page = new Page<>(pageNum, pageSize);
//        IPage<TsActivityDaily> result = iTsActivityDailyService.getActivityDetail(page, orgId, startDate, endDate, buildingId, targetType, targetId, targetName, StringUtils.humpToLine(asc), StringUtils.humpToLine(desc));
        List<TsActivityDaily>  result = iTsActivityDailyService.getActivityDetail(orgId, startDate, endDate, buildingId, targetType, targetId, targetName, StringUtils.humpToLine(asc), StringUtils.humpToLine(desc));

        return ResponseBean.success(result);
    }

    @PostMapping("/activity/detail")
    public ResponseBean getActivityDetail(// startDate 默认格式 YYYY-MM-DD
                                          @RequestParam(value ="startDate", required = false) String startDate,
                                          @RequestParam(value ="endDate", required = false) String endDate,
                                          @RequestParam(value ="buildingId", required = false) String buildingId,
                                          @RequestParam(value ="targetType", required = false) String targetType,
                                          @RequestParam(value ="targetId", required = false) String targetId,
                                          @RequestParam(value ="targetName", required = false) String targetName,
                                          HttpServletRequest request,
                                          HttpServletResponse response) {

        if (null == startDate || "".equals(startDate)) {
            return ResponseBean.error("开始日期不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }
        if (null == endDate || "".equals(endDate)) {
            endDate = DateUtils.today();
        }
        if (null == targetType || "".equals(targetType) || null == targetId || "".equals(targetId)) {
            return ResponseBean.error("活动类型或者ID 不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        List<ActivityDailyExport> result = iTsActivityDailyService.getActivityExport(orgId, startDate, endDate, buildingId, targetType, targetId);

        try {
            ExcelUtils.flush(response, ActivityDailyExport.class, result, StringUtils.strToUnicode(targetName + "活动统计"));
        } catch (IOException e) {
//            e.printStackTrace();
            return ResponseBean.error("导出Excel出错, 请重试 : " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
        }

        return null;
    }

}
