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

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.kuaishou.modules.report.service.IAccountReportService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

/**
 * @author jeecg-boot
 * 2019年12月5日19:55:14
 */
@Slf4j
@Api(tags = "广告主日报表")
@RestController
@RequestMapping("/ctop/accountReport")
public class AccountReportController {
    @Autowired
    private UserAllocationMapper userAllocationMapper;
    @Autowired
    private IAccountReportService accountReportService;


    @PostMapping(value = "/getAdvertiserList")
    public Result<List<UserAllocation>> queryPageList() {
        Result<List<UserAllocation>> result = new Result();

        try {
            QueryWrapper<UserAllocation> userAllocationQueryWrapper = new QueryWrapper<>();
            userAllocationQueryWrapper.eq("media_id", 2);
            userAllocationQueryWrapper.isNotNull("account_id");
            userAllocationQueryWrapper.groupBy("account_id");
            userAllocationQueryWrapper.orderByAsc("advertiser_name");
            List<UserAllocation> userAllocations = userAllocationMapper.selectList(userAllocationQueryWrapper);
            result.setSuccess(true);
            result.setMessage("查询成功");
            result.setResult(userAllocations);
        } catch (Exception e) {
            result.setSuccess(true);
            result.setMessage("查询失败");
            e.printStackTrace();

        }

        return result;
    }

    @PostMapping("/getReportList")
    public Result<JSONObject> getReportList(@RequestBody JSONObject json) {
        Result<JSONObject> result = new Result<>();
        try {
            JSONObject summaryJson = accountReportService.getSummaryReport(json);
            result.setResult(summaryJson);
            result.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;

    }


    @PostMapping("/getStatistics")
    public Result<JSONObject> getStatistics(@RequestBody JSONObject json) {
        Result<JSONObject> result = new Result<>();
        try {
            JSONObject statisticsJson = accountReportService.getStatistics(json);
            result.setResult(statisticsJson);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);

        }

        return result;

    }


}
