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.BytedanceDeliveryTargetTemplate;
import cn.com.ctop.toutiao.entity.BytedanceUrlInfo;
import cn.com.ctop.toutiao.mapper.BytedanceDeliveryTargetTemplateMapper;
import cn.com.ctop.toutiao.mapper.BytedanceUrlInfoMapper;
import cn.com.ctop.toutiao.service.IBytedanceDeliveryTargetTemplateService;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 投放目标模板信息
 * @Author: jeecg-boot
 * @Date: 2019-07-23
 * @Version: V1.0
 */
@Service
public class BytedanceDeliveryTargetTemplateServiceImpl extends ServiceImpl<BytedanceDeliveryTargetTemplateMapper, BytedanceDeliveryTargetTemplate> implements IBytedanceDeliveryTargetTemplateService {
    @Autowired
    BytedanceUrlInfoMapper urlInfoMapper;

    @Override
    public Map<String, Object> insertTemplate(BytedanceDeliveryTargetTemplate template) {
        Map<String, Object> resultMap = new HashMap<>();
        Long id = template.getId();
        String name = template.getName();
        if (null == name || "".equals(name.trim())) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_IS_NULL.getCode());
            return resultMap;
        }
        QueryWrapper<BytedanceDeliveryTargetTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", template.getName());
        if (null != id && id != 0) {
            queryWrapper.ne("id", id);
        }
        BytedanceDeliveryTargetTemplate templateExist = getOne(queryWrapper);
        if (null != templateExist) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_HAS_EXIST.getCode());
            return resultMap;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        template.setAdvertiserId(user.getId());
        BytedanceUrlInfo urlInfo = urlInfoMapper.selectById(template.getUrlId());
        //落地页
        if (null != urlInfo.getType() && "page".equals(urlInfo.getType())) {
            template.setExternalUrl(urlInfo.getUrl());
        } else {
            template.setDownloadUrl(urlInfo.getUrl());
            template.setPackageName(urlInfo.getPackageName());
        }
        template.setStatus(1);
        Date now = new Date();
        template.setCreateTime(now);
        template.setUpdateTime(now);
        this.saveOrUpdate(template);
        ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS.getCode());
        return resultMap;
    }

    @Override
    public Result<IPage<BytedanceDeliveryTargetTemplate>> getList(BytedanceDeliveryTargetTemplate template, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<BytedanceDeliveryTargetTemplate>> result = new Result<>();
        QueryWrapper<BytedanceDeliveryTargetTemplate> 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<BytedanceDeliveryTargetTemplate> page = new Page<>(pageNo, pageSize);
        IPage<BytedanceDeliveryTargetTemplate> pageList = this.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @Override
    public Map<String, Object> get(Long targetId) {
        Map<String, Object> result = new HashMap<>();
        if (null == targetId || targetId == 0) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR.getCode());
            return result;
        }
        BytedanceDeliveryTargetTemplate template = this.getById(targetId);
        if (null == template) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR.getCode());
            return result;
        }
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        result.put("data", template);
        return result;
    }
}
