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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.v3.entity.V3KuaishouAccountAutoTarget;
import cn.com.ctop.kuaishou.modules.v3.mapper.V3KuaishouAccountAutoTargetMapper;
import cn.com.ctop.kuaishou.modules.v3.service.V3KuaishouAccountAutoTargetService;
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.stereotype.Service;

import java.util.List;

/**
 * 自动投放-v3-本地定向表
 *
 * @author jeecg-boot
 * 2022-10-31
 * @version V1.0
 */
@Service
public class V3KuaishouAccountAutoTargetServiceImpl extends ServiceImpl<V3KuaishouAccountAutoTargetMapper, V3KuaishouAccountAutoTarget> implements V3KuaishouAccountAutoTargetService {

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

    @Override
    public Result<Object> addV3LocalTarget(JSONObject requestJson) {
        QueryWrapper<V3KuaishouAccountAutoTarget> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", requestJson.getLong("accountId"));
        wrapper.eq("target_name", requestJson.getString("targetName")).last("limit 1");
        V3KuaishouAccountAutoTarget one = this.getOne(wrapper);
        if (!Check.isNull(one)) {
            return Result.error("该账户下定向包名已存在");
        }
        V3KuaishouAccountAutoTarget target = JSONObject.toJavaObject(requestJson, V3KuaishouAccountAutoTarget.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> updateV3LocalTarget(JSONObject requestJson) {
        QueryWrapper<V3KuaishouAccountAutoTarget> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", requestJson.getLong("accountId"));
        wrapper.eq("target_name", requestJson.getString("targetName")).last("limit 1");
        V3KuaishouAccountAutoTarget one = this.getOne(wrapper);
        if (!Check.isNull(one) && one.getId() - requestJson.getLong("id") != 0) {
            return Result.error("账户下定向包名重复");
        }
        V3KuaishouAccountAutoTarget target = JSONObject.toJavaObject(requestJson, V3KuaishouAccountAutoTarget.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<V3KuaishouAccountAutoTarget> getAutoTargetByIds(List<String> list) {
        return baseMapper.getAutoTargetByIds(list);
    }
}
