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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.live.entity.LiveDataImportRecord;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 直播数据——操作记录表
 */
@Slf4j
@Api(tags = "直播数据——操作记录表")
@RestController
@RequestMapping("/liveData/importRecord")
public class LiveDataImportRecordController {
    @Autowired
    private ILiveDataImportRecordService liveDataImportRecordService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<LiveDataImportRecord>> queryPageList(LiveDataImportRecord liveDataImportRecord,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        Result<IPage<LiveDataImportRecord>> result = new Result<>();
        QueryWrapper<LiveDataImportRecord> queryWrapper = QueryGenerator.initQueryWrapper(new LiveDataImportRecord(), req.getParameterMap());
        Page<LiveDataImportRecord> page = new Page<LiveDataImportRecord>(pageNo, pageSize);
        if (!Check.isNull(liveDataImportRecord.getAccountId())) {
                queryWrapper.eq("account_id", liveDataImportRecord.getAccountId());
        }
        if (!Check.isNull(liveDataImportRecord.getStatDate())) {
            queryWrapper.eq("stat_date", liveDataImportRecord.getStatDate());
        }
        if (!Check.isNull(liveDataImportRecord.getType())) {
            queryWrapper.eq("type", liveDataImportRecord.getType());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<LiveDataImportRecord> pageList = liveDataImportRecordService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<LiveDataImportRecord> add(@RequestBody LiveDataImportRecord liveDataImportRecord) {
        Result<LiveDataImportRecord> result = new Result<>();
        try {
            liveDataImportRecordService.save(liveDataImportRecord);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

}
