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.KuaishouPopulationGroup;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouPopulationGroupMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouPopulationGroupService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 人群包-广告组关联表
 *
 * @author jeecg-boot
 * 2021-05-08
 * @version V1.0
 */
@Service
public class KuaishouPopulationGroupServiceImpl extends ServiceImpl<KuaishouPopulationGroupMapper, KuaishouPopulationGroup> implements IKuaishouPopulationGroupService {

    @Autowired
    private KuaishouPopulationGroupMapper populationGroupMapper;

    @Override
    public void createPopulationAndGroupRel(JSONObject request) throws Exception {
        try {
            List<KuaishouPopulationGroup> list = new ArrayList<>();
            Long accountId = request.getLong("accountId");
            Long unitId = request.getLong("unitId");
            KuaishouPopulationGroup populationGroup = null;
            JSONArray population = request.getJSONArray("population");
            if (!Check.isNull(population)) {
                for (Object id : population) {
                    populationGroup = new KuaishouPopulationGroup();
                    populationGroup.setAccountId(accountId);
                    populationGroup.setUnitId(unitId);
                    populationGroup.setOrientationId(Long.valueOf(String.valueOf(id)));
                    list.add(populationGroup);
                }
            }
            JSONArray excludePopulation = request.getJSONArray("excludePopulation");
            if (!Check.isNull(excludePopulation)) {
                for (Object id : excludePopulation) {
                    populationGroup = new KuaishouPopulationGroup();
                    populationGroup.setAccountId(accountId);
                    populationGroup.setUnitId(unitId);
                    populationGroup.setOrientationId(Long.valueOf(String.valueOf(id)));
                    list.add(populationGroup);
                }
            }
            JSONArray paidAudience = request.getJSONArray("paidAudience");
            if (!Check.isNull(paidAudience)) {
                for (Object id : paidAudience) {
                    populationGroup = new KuaishouPopulationGroup();
                    populationGroup.setAccountId(accountId);
                    populationGroup.setUnitId(unitId);
                    populationGroup.setOrientationId(Long.valueOf(String.valueOf(id)));
                    list.add(populationGroup);
                }
            }
            if (list.size() > 0) {
                populationGroupMapper.deleteByUnitId(request.getLong("unitId"));
                populationGroupMapper.replaceBatch(list);
            }
        } catch (NumberFormatException e) {
            log.error("添加广告组和人群包绑定关系异常", e);
        }
    }
}
