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

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.toutiao.entity.ByteDanceUserOrientationTemplate;
import cn.com.ctop.toutiao.mapper.ByteDanceUserOrientationTemplateMapper;
import cn.com.ctop.toutiao.service.IByteDanceUserOrientationTemplateService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 今日头条受众模板信息
 * @Author: jeecg-boot
 * @Date: 2019-07-23
 * @Version: V1.0
 */
@Service
public class ByteDanceUserOrientationTemplateServiceImpl extends ServiceImpl<ByteDanceUserOrientationTemplateMapper, ByteDanceUserOrientationTemplate> implements IByteDanceUserOrientationTemplateService {
    @Autowired
    private ByteDanceUserOrientationTemplateMapper templateMapper;

    @Override
    public Map<String, Object> insertTemplate(JSONObject template) {
        Map<String, Object> resultMap = new HashMap<>();
        Long id = template.getLong("id");
        String name = template.getString("name");
        if (null == name || "".equals(name.trim())) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_IS_NULL.getCode());
            return resultMap;
        }
        QueryWrapper<ByteDanceUserOrientationTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (null != id && id != 0) {
            queryWrapper.ne("id", id);
        }
        ByteDanceUserOrientationTemplate templateExist = getOne(queryWrapper);
        if (null != templateExist) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_HAS_EXIST.getCode());
            return resultMap;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ByteDanceUserOrientationTemplate orientationTemplate = new ByteDanceUserOrientationTemplate(user.getId(), template);
        this.saveOrUpdate(orientationTemplate);
        ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS.getCode());
        return resultMap;
    }

    @Override
    public Result<IPage<ByteDanceUserOrientationTemplate>> getList(ByteDanceUserOrientationTemplate template, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<ByteDanceUserOrientationTemplate>> result = new Result<>();
        QueryWrapper<ByteDanceUserOrientationTemplate> queryWrapper = QueryGenerator.initQueryWrapper(template, req.getParameterMap());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String advertiserId = user.getId();
        queryWrapper.eq("advertiser_id", advertiserId).orderByDesc("create_time");
        Page<ByteDanceUserOrientationTemplate> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceUserOrientationTemplate> pageList = this.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
}
