package com.huiju.estateagents.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.TaPoster;
import com.huiju.estateagents.entity.TaPosterUsed;
import com.huiju.estateagents.mapper.TaPosterMapper;
import com.huiju.estateagents.mapper.TaPosterUsedMapper;
import com.huiju.estateagents.service.TaPosterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分享海报表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-09-21
 */
@Service
public class TaPosterServiceImpl extends ServiceImpl<TaPosterMapper, TaPoster> implements TaPosterService {

    @Autowired
    TaPosterMapper taPosterMapper;

    @Autowired
    private TaPosterUsedMapper taPosterUsedMapper;

    @Override
    public List<TaPoster> getPostersForTarget(String targetId, String targetType){
        QueryWrapper<TaPoster>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("target_id",targetId);
        queryWrapper.eq("target_type",targetType);
        queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
        List<TaPoster> data =taPosterMapper.selectList(queryWrapper);
        return data;

    }
    
    /**
     * 获取海报和已使用的海报模板
     *
     * @param targetId
     * @param targetType
     * @return
     */
    @Override
    public TaPoster getUsedPostersForTarget(String targetId, String targetType) {
        QueryWrapper<TaPoster>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("target_id",targetId);
        queryWrapper.eq("target_type",targetType);
        queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
        TaPoster taPoster =taPosterMapper.selectOne(queryWrapper);
        if (null != taPoster){
            //获取使用的模板集合
            QueryWrapper<TaPosterUsed> usedQueryWrapper = new QueryWrapper<>();
            usedQueryWrapper.eq("poster_id",taPoster.getPosterId());
            usedQueryWrapper.eq("org_id",taPoster.getOrgId());
            List<TaPosterUsed> taPosterUsedList = taPosterUsedMapper.selectList(usedQueryWrapper);
            List<Integer> checkedList = new ArrayList<>();
            taPosterUsedList.forEach(e -> {
                checkedList.add(e.getTemplateId());
            });
            taPoster.setTemplateIds(checkedList);
        }
        return taPoster;
    }
    
}
