package cn.com.ctop.kuaishou.modules.live.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.live.entity.LiveDataAd;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataAdService;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataImportRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 直播数据——广告人群报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-04-06
 */
@Slf4j
@Api(tags = "直播数据——广告人群报表")
@RestController
@RequestMapping("/liveData/ad")
public class LiveDataAdController {
    @Autowired
    private ILiveDataAdService liveDataAdService;

    @Autowired
    private ILiveDataImportRecordService recordService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<LiveDataAd>> queryPageList(LiveDataAd liveDataAd,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        Result<IPage<LiveDataAd>> result = new Result<>();
        QueryWrapper<LiveDataAd> queryWrapper = QueryGenerator.initQueryWrapper(new LiveDataAd(), req.getParameterMap());
        Page<LiveDataAd> page = new Page<LiveDataAd>(pageNo, pageSize);
        if (!Check.isNull(liveDataAd.getAccountId())) {
            queryWrapper.eq("account_id", liveDataAd.getAccountId());
        }
        if (!Check.isNull(liveDataAd.getLiveStartTime())) {
            queryWrapper.ge("live_start_time", liveDataAd.getLiveStartTime());
        }

        if (!Check.isNull(liveDataAd.getLiveEndTime())) {
            queryWrapper.le("live_start_time", liveDataAd.getLiveEndTime());
        }

        if (!Check.isNull(liveDataAd.getLiveId())) {
            queryWrapper.eq("live_id", liveDataAd.getLiveId());
        }
        queryWrapper.eq("state", 1);
        queryWrapper.orderByDesc("create_time");
        IPage<LiveDataAd> pageList = liveDataAdService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * 导入EXCEL批量创建
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result<Object> importExcel(HttpServletResponse response, String userId, Long accountId, @RequestParam("file") MultipartFile file) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            if (Check.isNull(file)) {
                return Result.error("执行失败，请上传应用Excel文件");
            }
            if (Check.isNull(accountId) || Check.isNull(userId)) {
                return Result.error("缺少参数");
            }
            Result<Object> result = liveDataAdService.importExcel(accountId, file);
            recordService.addImportRecord(accountId, 7, userId);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入EXCEL失败，" + e.getMessage());
        }
    }


    /**
     * 通过id查询
     */
    @GetMapping(value = "/queryById")
    public Result<LiveDataAd> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<LiveDataAd> result = new Result<>();
        LiveDataAd liveDataAd = liveDataAdService.getById(id);
        if (liveDataAd == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(liveDataAd);
            result.setSuccess(true);
        }
        return result;
    }

}
