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.ByteDanceBudgetTemplate;
import cn.com.ctop.toutiao.mapper.ByteDanceBudgetTemplateMapper;
import cn.com.ctop.toutiao.service.IByteDanceBudgetTemplateService;
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.stereotype.Service;

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

/**
 * @Description: 今日头条预算模板信息
 * @Author: jeecg-boot
 * @Date: 2019-07-22
 * @Version: V1.0
 */
@Service
public class ByteDanceBudgetTemplateServiceImpl extends ServiceImpl<ByteDanceBudgetTemplateMapper, ByteDanceBudgetTemplate> implements IByteDanceBudgetTemplateService {

    @Override
    public Map<String, Object> insertTemplate(ByteDanceBudgetTemplate template) {
        Map<String, Object> resultMap = new HashMap<>();
        String name = template.getName();
        Long id = template.getId();
        if (null == name || "".equals(name.trim())) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_IS_NULL.getCode());
            return resultMap;
        }
        QueryWrapper<ByteDanceBudgetTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", template.getName());
        if (null != id && id != 0) {
            queryWrapper.ne("id", template.getId());
        }
        List<ByteDanceBudgetTemplate> templateExistList = this.list(queryWrapper);
        if (templateExistList != null && templateExistList.size() > 0) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.TEMPLATE_NAME_HAS_EXIST.getCode());
            return resultMap;
        }
        template.setStatus(1);
        if (template.getStartDate() != null) {
            template.setStartDate(template.getStartDate().substring(0, 10));
        }
        if (template.getEndDate() != null) {
            template.setEndDate(template.getEndDate().substring(0, 10));
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        template.setAdvertiserId(user.getId());
        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<ByteDanceBudgetTemplate>> getList(ByteDanceBudgetTemplate template, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<ByteDanceBudgetTemplate>> result = new Result<>();
        QueryWrapper<ByteDanceBudgetTemplate> 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<ByteDanceBudgetTemplate> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceBudgetTemplate> pageList = this.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
}
