package com.ruixuan.launch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.Check;
import com.ruixuan.launch.entity.KuaishouLaunchCampaignAutoStrategy;
import com.ruixuan.launch.entity.KuaishouLaunchTargetTemplate;
import com.ruixuan.launch.mapper.KuaishouLaunchCampaignAutoStrategyMapper;
import com.ruixuan.launch.mapper.KuaishouLaunchTargetTemplateMapper;
import com.ruixuan.launch.service.IKuaishouLaunchTargetTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账户下所属定向信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-05
 */
@Service
public class KuaishouLaunchTargetTemplateServiceImpl implements IKuaishouLaunchTargetTemplateService {
    @Autowired
    private KuaishouLaunchTargetTemplateMapper kuaishouLaunchTargetTemplateMapper;

    @Autowired
    private KuaishouLaunchCampaignAutoStrategyMapper autoStrategyMapper;


    /**
     * 查询账户下所属定向信息
     *
     * @param id 账户下所属定向信息主键
     * @return 账户下所属定向信息
     */
    @Override
    public KuaishouLaunchTargetTemplate selectKuaishouLaunchTargetTemplateById(Long id) {
        return kuaishouLaunchTargetTemplateMapper.selectKuaishouLaunchTargetTemplateById(id);
    }

    /**
     * 查询账户下所属定向信息列表
     *
     * @param kuaishouLaunchTargetTemplate 账户下所属定向信息
     * @return 账户下所属定向信息
     */
    @Override
    public List<KuaishouLaunchTargetTemplate> selectKuaishouLaunchTargetTemplateList(KuaishouLaunchTargetTemplate kuaishouLaunchTargetTemplate) {
        return kuaishouLaunchTargetTemplateMapper.selectKuaishouLaunchTargetTemplateList(kuaishouLaunchTargetTemplate);
    }

    /**
     * 新增账户下所属定向信息
     *
     * @param kuaishouLaunchTargetTemplate 账户下所属定向信息
     * @return 结果
     */
    @Override
    public ResultResponse insertKuaishouLaunchTargetTemplate(JSONObject request) {
        KuaishouLaunchTargetTemplate kuaishouLaunchTargetTemplate = JSONObject.parseObject(request.toJSONString(), KuaishouLaunchTargetTemplate.class);
        KuaishouLaunchTargetTemplate one = kuaishouLaunchTargetTemplateMapper.getOne(kuaishouLaunchTargetTemplate.getAccountId(), kuaishouLaunchTargetTemplate.getTargetName(), kuaishouLaunchTargetTemplate.getTargetType());
        kuaishouLaunchTargetTemplateMapper.insertKuaishouLaunchTargetTemplate(kuaishouLaunchTargetTemplate);
        return ResultResponse.success();
    }

    /**
     * 修改账户下所属定向信息
     *
     * @param kuaishouLaunchTargetTemplate 账户下所属定向信息
     * @return 结果
     */
    @Override
    public int updateKuaishouLaunchTargetTemplate(KuaishouLaunchTargetTemplate kuaishouLaunchTargetTemplate) {
        return kuaishouLaunchTargetTemplateMapper.updateKuaishouLaunchTargetTemplate(kuaishouLaunchTargetTemplate);
    }

    /**
     * 批量删除账户下所属定向信息
     *
     * @param ids 需要删除的账户下所属定向信息主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLaunchTargetTemplateByIds(Long[] ids) {
        return kuaishouLaunchTargetTemplateMapper.deleteKuaishouLaunchTargetTemplateByIds(ids);
    }

    /**
     * 删除账户下所属定向信息信息
     *
     * @param id 账户下所属定向信息主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLaunchTargetTemplateById(Long id) {
        return kuaishouLaunchTargetTemplateMapper.deleteKuaishouLaunchTargetTemplateById(id);
    }

    @Override
    public JSONObject getTargetJson(Long targetId, JSONObject strategyJson) {
        KuaishouLaunchTargetTemplate template = kuaishouLaunchTargetTemplateMapper.selectKuaishouLaunchTargetTemplateById(targetId);
        JSONObject targetJson = (JSONObject) JSONObject.toJSON(template);
        targetJson.remove("id");
        strategyJson.putAll(targetJson);
        return strategyJson;
    }

    @Override
    public ResultResponse selectKuaishouLaunchTargetTemplateByIds(String ids) {
        List<Long> idList = JSONArray.parseArray(ids, Long.class);
        List<KuaishouLaunchTargetTemplate> list = kuaishouLaunchTargetTemplateMapper.selectKuaishouLaunchTargetTemplateByIds(idList);
        return ResultResponse.success(list);
    }

    @Override
    public ResultResponse insertMore(JSONObject request) {
        JSONArray array = request.getJSONArray("array");
        JSONArray temIds = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            JSONObject entity = array.getJSONObject(i);
            KuaishouLaunchTargetTemplate template = JSONObject.parseObject(entity.toJSONString(), KuaishouLaunchTargetTemplate.class);
            if (Check.isNull(template.getId())) {
                kuaishouLaunchTargetTemplateMapper.insertKuaishouLaunchTargetTemplate(template);
                temIds.add(template.getId());
            } else {
                temIds.add(template.getId());
                kuaishouLaunchTargetTemplateMapper.updateKuaishouLaunchTargetTemplate(template);
            }
        }
        if (Check.isNull(temIds)) {
            request.put("targetTemplate", "[]");
        } else {
            request.put("targetTemplate", temIds);
        }
        KuaishouLaunchCampaignAutoStrategy strategy = JSONObject.parseObject(request.toJSONString(), KuaishouLaunchCampaignAutoStrategy.class);
        autoStrategyMapper.updateKuaishouLaunchCampaignAutoStrategy(strategy);


        return ResultResponse.success();
    }

}