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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchCreativePreview;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchGroupPreview;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouBatchCreativePreviewMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouBatchGroupPreviewMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouBatchCreativePreviewService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouBatchGroupPreviewService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 快手-广告组预览信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-01-26
 */
@Service
public class KuaishouBatchGroupPreviewServiceImpl extends ServiceImpl<KuaishouBatchGroupPreviewMapper, KuaishouBatchGroupPreview> implements IKuaishouBatchGroupPreviewService {

    @Autowired
    KuaishouBatchGroupPreviewMapper groupPreviewMapper;
    @Autowired
    KuaishouBatchCreativePreviewMapper creativePreviewMapper;
    @Autowired
    private IKuaishouBatchCreativePreviewService kuaishouBatchCreativePreview;

    @Override
    public Result<Object> batchEdit(JSONObject data) {
        JSONArray groupIds = data.getJSONArray("groupIds");
        if (Check.isNull(groupIds)) {
            Result.error("请选择广告组");
        }
        for (int i = 0; i < groupIds.size(); i++) {
            KuaishouBatchCreativePreview creative = new KuaishouBatchCreativePreview();
            QueryWrapper<KuaishouBatchCreativePreview> wrapper = new QueryWrapper<>();
            creative.setClickTrackUrl(data.getString("clickTrackUrl"));
            creative.setActionbarClickUrl(data.getString("actionbarClickUrl"));
            wrapper.eq("group_id", groupIds.getString(i));
            if (!Check.isNull(creative.getClickTrackUrl()) || !Check.isNull(creative.getActionbarClickUrl())) {
                kuaishouBatchCreativePreview.update(creative, wrapper);
            }
            KuaishouBatchGroupPreview groupPreview = new KuaishouBatchGroupPreview();
            QueryWrapper<KuaishouBatchGroupPreview> groupWrapper = new QueryWrapper<>();
            groupPreview.setBid(data.getInteger("bid"));
            groupPreview.setCpaBid(data.getInteger("cpaBid"));
            groupPreview.setDayBudget(data.getInteger("dayBudget"));
            groupPreview.setDeepConversionBid(data.getInteger("deepConversionBid"));
            groupWrapper.eq("id", groupIds.getString(i));
            if (!Check.isNull(groupPreview.getBid()) || !Check.isNull(groupPreview.getCpaBid()) || !Check.isNull(groupPreview.getDayBudget())) {
                groupPreviewMapper.update(groupPreview, groupWrapper);
            }
        }
        return Result.ok("修改成功");
    }
}
