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

import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaiShouStrategyListPageMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaiShouStrategyListPageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AiKuaiShouStrategyListPageServiceImpl implements IAiKuaiShouStrategyListPageService {

    @Autowired
    AiKuaiShouStrategyListPageMapper aiKuaiShouStrategyListPageMapper;

    @Autowired
    IUserAllocationService userAllocationService;

    @Override
    public PageInfo<JSONObject> pageStrategy(JSONObject params) {
        JSONArray accountIds= params.getJSONArray("accountIds");
        if(null == accountIds||accountIds.isEmpty()){
            accountIds=listToJSONArray(userAllocationService.getAccountIdsByUserId(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getId()));
        }
        String strategyName=params.getString("strategyName");
        Integer strategyState=params.getInteger("strategyState");
        Long id=params.getLong("id");
        int pageNo=params.getInteger("pageNo");
        int pageSize=params.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list=aiKuaiShouStrategyListPageMapper.queryStrategyBy(accountIds,strategyName,strategyState,id);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<JSONObject> pageCampaign(JSONObject params) {
        Long strategyId= params.getLong("strategyId");
        if(null==strategyId){
            log.error("参数异常，strategyId is no present");
            return null;
        }
        String campaignName=params.getString("campaignName");
        Integer status=params.getInteger("status");
        Long campaignId=params.getLong("campaignId");
        int pageNo=params.getInteger("pageNo");
        int pageSize=params.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list=aiKuaiShouStrategyListPageMapper.queryCampaignByStrategyId(strategyId,campaignName,campaignId,status);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<JSONObject> pageUnit(JSONObject params) {
        Long campaignId= params.getLong("campaignId");
        if(null==campaignId){
            log.error("参数异常，campaignId is no present");
            return null;
        }
        String unitName=params.getString("unitName");
        Integer status=params.getInteger("status");
        Long unitId=params.getLong("unitId");
        int pageNo=params.getInteger("pageNo");
        int pageSize=params.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> list=aiKuaiShouStrategyListPageMapper.queryUnitByCampaignId(campaignId,unitName,unitId,status);
        return new PageInfo<>(list);
    }

    private JSONArray listToJSONArray(List<JSONObject> data){
        JSONArray result=new JSONArray();
        data.forEach(jsonObject->result.add(jsonObject.getLong("account_id")));
        return result;
    }
}
