package cn.com.ctop.toutiao.modules.material.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.StringUtils;
import cn.com.ctop.toutiao.modules.material.entity.BytedanceVideoSlogenInfo;
import cn.com.ctop.toutiao.modules.material.mapper.BytedanceVideoSlogenInfoMapper;
import cn.com.ctop.toutiao.modules.material.service.IBytedanceVideoSlogenInfoService;
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.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 头条视频文案信息
 * @author jeecg-boot
 * 2021-04-19
 * @version V1.0
 */
@Service
public class BytedanceVideoSlogenInfoServiceImpl extends ServiceImpl<BytedanceVideoSlogenInfoMapper, BytedanceVideoSlogenInfo> implements IBytedanceVideoSlogenInfoService {
    @Resource
    BytedanceVideoSlogenInfoMapper bytedanceVideoSlogenInfoMapper;

    @Override
    public void insertSlogans(Long projectId, String videoCode, JSONArray slogans,String userId){
        if(null==slogans||slogans.isEmpty()){
            return;
        }
        for(int i=0;i<slogans.size();i++){
            JSONObject sloganObject = slogans.getJSONObject(i);
            String title = sloganObject.getString("title");
            title = title.replaceAll("\\{\\{","{");
            title = title.replaceAll("\\}\\}","}");
            String creativeWordIds = Check.isNull(sloganObject.getJSONArray("creative_word_ids")) ? "" : sloganObject.getJSONArray("creative_word_ids").toJSONString();
            BytedanceVideoSlogenInfo slogenInfo = new BytedanceVideoSlogenInfo(projectId,videoCode,title,userId,creativeWordIds);
            this.save(slogenInfo);
        }
    }

    @Override
    public void deleteAllSlogens(Long projectId, String videoCode, String userId){
        bytedanceVideoSlogenInfoMapper.updateStatusToZero(projectId, videoCode, userId);
    }

    @Override
    public List<BytedanceVideoSlogenInfo> listByParams(String code, Integer status) {
        QueryWrapper<BytedanceVideoSlogenInfo>queryWrapper =new QueryWrapper<>();
        if(!Check.isNull(code)){
            queryWrapper.eq("video_code",code);
        }
        if(!Check.isNull(status)){
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }
}
