package cn.com.ctop.kuaishou.modules.ai.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouProjectStrategy;
import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouAccountLevelOperationRecordMapper;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouProjectStrategyMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouProjectStrategyService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 自动投放-项目策略表
 *
 * @author jeecg-boot
 * 2021-06-24
 * @version V1.0
 */
@Service
public class KuaishouProjectStrategyServiceImpl extends ServiceImpl<KuaishouProjectStrategyMapper, KuaishouProjectStrategy> implements IKuaishouProjectStrategyService {

    @Autowired
    private KuaishouProjectStrategyMapper kuaishouProjectStrategyMapper;

    @Autowired
    private AiKuaishouAccountLevelOperationRecordMapper kuaishouAccountLevelOperationRecordMapper;

    @Override
    public Result<Object> queryProjectList(JSONObject data) {
        List<Long> projectIds = new ArrayList<>();
        if (Check.isNull(data.getLong("projectId"))) {
            projectIds = kuaishouProjectStrategyMapper.queryProjectId();
        } else {
            projectIds.add(data.getLong("projectId"));
        }
        data.put("projectIds", projectIds);
        JSONObject result = new JSONObject();
        JSONObject allInfo = kuaishouProjectStrategyMapper.queryProjectAll(data);
        Integer pageNo = Check.isNull(data.getInteger("pageNo")) ? 1 : data.getInteger("pageNo");
        Integer pageSize = Check.isNull(data.getInteger("pageSize")) ? 10 : data.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list = kuaishouProjectStrategyMapper.queryProjectList(data);
        result.put("total", allInfo);
        result.put("pageInfo", new PageInfo<>(list));
        return Result.ok(result);
    }

    @Override
    public Result<Object> queryAccountList(JSONObject data) {
        List<Long> projectIds = new ArrayList<>();
        if (Check.isNull(data.getLong("projectId"))) {
            projectIds = kuaishouProjectStrategyMapper.queryProjectId();
        } else {
            projectIds.add(data.getLong("projectId"));
        }
        data.put("projectIds", projectIds);
        JSONObject result = new JSONObject();
        JSONObject allInfo = kuaishouProjectStrategyMapper.queryAccountAll(data);
        Integer pageNo = Check.isNull(data.getInteger("pageNo")) ? 1 : data.getInteger("pageNo");
        Integer pageSize = Check.isNull(data.getInteger("pageSize")) ? 10 : data.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list = kuaishouProjectStrategyMapper.queryAccountList(data);
        result.put("total", allInfo);
        result.put("pageInfo", new PageInfo<>(list));
        return Result.ok(result);
    }

    @Override
    public Result<Object> queryMatchResult(Long projectId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list = kuaishouAccountLevelOperationRecordMapper.queryMatchResult(projectId);
        return Result.ok(new PageInfo<>(list));
    }

    @Override
    public boolean checkProjectisOpen(Long projectId) {
        Integer num = kuaishouProjectStrategyMapper.checkProjectisOpen(projectId);
        return num <= 0;
    }

    @Override
    public List<KuaishouProjectStrategy> getAllEffectStrategy(int unitType, String sourceMaterial) {
        return kuaishouProjectStrategyMapper.getAllEffectStrategy(unitType, sourceMaterial);
    }
}
