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


import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.live.entity.LiveDataAge;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataAgeService;
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 io.swagger.annotations.ApiOperation;
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/age")
public class LiveDataAgeController {
    @Autowired
    private ILiveDataAgeService liveDataAgeService;

    @Autowired
    private ILiveDataImportRecordService recordService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<LiveDataAge>> queryPageList(LiveDataAge liveDataAge,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<LiveDataAge>> result = new Result<>();
        QueryWrapper<LiveDataAge> queryWrapper = QueryGenerator.initQueryWrapper(new LiveDataAge(), req.getParameterMap());
        Page<LiveDataAge> page = new Page<LiveDataAge>(pageNo, pageSize);
        if (!Check.isNull(liveDataAge.getAccountId())) {
            queryWrapper.eq("account_id", liveDataAge.getAccountId());
        }
        if (!Check.isNull(liveDataAge.getStatMonth())) {
            queryWrapper.eq("stat_month", liveDataAge.getStatMonth());
        }
        if (!Check.isNull(liveDataAge.getAge())) {
            queryWrapper.eq("age", liveDataAge.getAge());
        }
        queryWrapper.eq("state", 1);
        queryWrapper.orderByDesc("create_time").orderByAsc("age");
        IPage<LiveDataAge> pageList = liveDataAgeService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * 导入EXCEL批量创建
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result<Object> importExcel(HttpServletResponse response, String userId, String statMonth, 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) || Check.isNull(statMonth)) {
                return Result.error("缺少参数");
            }
            Result<Object> result = liveDataAgeService.importExcel(accountId, statMonth, file);
            recordService.addImportRecord(accountId, 4, userId);
            return result;

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


    /**
     * 通过id查询
     */
    @ApiOperation(value = "直播数据——各年龄段人群报表-通过id查询", notes = "直播数据——各年龄段人群报表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LiveDataAge> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<LiveDataAge> result = new Result<>();
        LiveDataAge liveDataAge = liveDataAgeService.getById(id);
        if (liveDataAge == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(liveDataAge);
            result.setSuccess(true);
        }
        return result;
    }
}
