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.AiKuaishouAccountAutoTarget;
import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouAccountAutoTargetMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountAutoTargetService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自动投放重构-本地定向表
 *
 * @author jeecg-boot
 * 2021-10-12
 * @version V1.0
 */
@Service
public class AiKuaishouAccountAutoTargetServiceImpl extends ServiceImpl<AiKuaishouAccountAutoTargetMapper, AiKuaishouAccountAutoTarget> implements IAiKuaishouAccountAutoTargetService {

    @Autowired
    private AiKuaishouAccountAutoTargetMapper accountAutoTargetMapper;

    @Override
    public Result<Object> queryPageList(Long accountId, String targetName,Integer platformOs, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<AiKuaishouAccountAutoTarget> list = accountAutoTargetMapper.queryPageList(accountId, targetName,platformOs);
        return Result.ok(new PageInfo<>(list));
    }

    @Override
    public Result<Object> addLocalTarget(JSONObject requestJson) {
        QueryWrapper<AiKuaishouAccountAutoTarget> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", requestJson.getLong("accountId"));
        wrapper.eq("target_name", requestJson.getString("targetName")).last("limit 1");
        AiKuaishouAccountAutoTarget one = this.getOne(wrapper);
        if (!Check.isNull(one)) {
            return Result.error("该账户下定向包名已存在");
        }
        AiKuaishouAccountAutoTarget target = JSONObject.toJavaObject(requestJson, AiKuaishouAccountAutoTarget.class);
        if (!Check.isNull(requestJson.getJSONArray("keyword"))) {
            target.setKeyword(JSONObject.toJSONString(requestJson.getJSONArray("keyword")));
        }
        boolean save = this.save(target);
        if (save) {
            return Result.ok("创建成功");
        }
        return Result.error("创建失败，定向包新增失败");
    }

    @Override
    public Result<Object> updateLocalTarget(JSONObject requestJson) {
        QueryWrapper<AiKuaishouAccountAutoTarget> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", requestJson.getLong("accountId"));
        wrapper.eq("target_name", requestJson.getString("targetName")).last("limit 1");
        AiKuaishouAccountAutoTarget one = this.getOne(wrapper);
        if (!Check.isNull(one) && one.getId() - requestJson.getLong("id") != 0) {
            return Result.error("账户下定向包名重复");
        }
        AiKuaishouAccountAutoTarget target = JSONObject.toJavaObject(requestJson, AiKuaishouAccountAutoTarget.class);
        if (!Check.isNull(requestJson.getJSONArray("keyword"))) {
            target.setKeyword(JSONObject.toJSONString(requestJson.getJSONArray("keyword")));
        }
        if (!Check.isNull(target.getMax()) && target.getMax() == -1) {
            target.setMax(null);
        }
        if (!Check.isNull(target.getMin()) && target.getMin() == -1) {
            target.setMin(null);
        }
        boolean update = this.saveOrUpdate(target);
        if (update) {
            return Result.ok("修改成功");
        }
        return Result.ok("修改失败");
    }

    @Override
    public List<AiKuaishouAccountAutoTarget> getAutoTargetByIds(List<String> list) {
        return accountAutoTargetMapper.getAutoTargetByIds(list);
    }

}
