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.CollectionSlogans;
import cn.com.ctop.kuaishou.modules.batch.mapper.CollectionSlogansMapper;
import cn.com.ctop.kuaishou.modules.batch.service.ICollectionSlogansService;
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 com.xxl.job.core.enums.NoEn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 收藏的广告语
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-10-12
 */
@Service
public class CollectionSlogansServiceImpl extends ServiceImpl<CollectionSlogansMapper, CollectionSlogans> implements ICollectionSlogansService {

    @Autowired
    private CollectionSlogansMapper collectionSlogansMapper;

    @Override
    public void saveCollectionSlogans(JSONObject requestJson) {
        CollectionSlogans co = new CollectionSlogans();
        String userId = requestJson.getString("userId");
        Long accountId = requestJson.getLong("accountId");
        JSONArray coverSlogansList = requestJson.getJSONArray("coverSlogansList");
        JSONArray captionsList = requestJson.getJSONArray("captionsList");
        JSONArray tagList = requestJson.getJSONArray("creativeTag");
        if (!Check.isNull(coverSlogansList)) {
            //封面广告语
            co.setType(NoEn.NO1.valueStr());
            co.setValue(coverSlogansList.toString());
        }
        if (!Check.isNull(captionsList)) {
            co.setType(NoEn.NO2.valueStr());
            co.setValue(captionsList.toString());
        }
        //创意标题
        if (!Check.isNull(tagList)) {
            co.setType(NoEn.NO3.valueStr());
            co.setValue(tagList.toString());
        }
        co.setAccountId(accountId);
        co.setUserId(userId);
        JSONObject slogansJson = getByUserId(userId, accountId.toString(), co.getType());
        if (!Check.isNull(slogansJson)) {
            QueryWrapper<CollectionSlogans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", slogansJson.getInteger("id"));
            //如果入参中type为delete，则只做修改操作，否则需要判断已有数据和入参数据不能重复
            if (!"delete".equals(requestJson.getString("type"))) {
                JSONArray value = slogansJson.getJSONArray("value");
                if (NoEn.NO1.valueStr().equals(co.getType())) {
                    for (int i = 0; i < coverSlogansList.size(); i++) {
                        if (!value.contains(coverSlogansList.get(i))) {
                            value.add(coverSlogansList.get(i));
                        }
                    }
                    co.setValue(value.toString());
                } else if (NoEn.NO2.valueStr().equals(co.getType())) {
                    for (int i = 0; i < captionsList.size(); i++) {
                        if (!value.contains(captionsList.get(i))) {
                            value.add(captionsList.get(i));
                        }
                    }
                    co.setValue(value.toString());
                } else {
                    for (int i = 0; i < tagList.size(); i++) {
                        if (!value.contains(tagList.get(i))) {
                            value.add(tagList.get(i));
                        }
                    }
                    co.setValue(value.toString());
                }
            }
            collectionSlogansMapper.update(co, queryWrapper);
        } else {
            collectionSlogansMapper.insert(co);
        }
    }

    @Override
    public JSONObject getByUserId(String userId, String accountId, String type) {
        QueryWrapper<CollectionSlogans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("type", type);
        queryWrapper.last("limit 1");
        CollectionSlogans slogans = collectionSlogansMapper.selectOne(queryWrapper);
        JSONObject resultJson = null;
        if (!Check.isNull(slogans)) {
            resultJson = (JSONObject) JSONObject.toJSON(slogans);
            resultJson.put("value", JSONArray.parseArray(slogans.getValue()));
        }
        return resultJson;
    }
}
