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.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.drainage.entity.TaDrainage;
import com.huiju.estateagents.drainage.mapper.TaDrainageMapper;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.mapper.*;
import com.huiju.estateagents.service.IExtendContentService;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 辅助内容表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@Service
public class ExtendContentServiceImpl extends ServiceImpl<ExtendContentMapper, ExtendContent> implements IExtendContentService {

    @Autowired
    private ExtendContentMapper extendContentMapper;
    @Autowired
    private TaBuildingMapper taBuildingMapper;
    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;
    @Autowired
    private TaNewsMapper taNewsMapper;
    @Autowired
    private HelpActivityMapper helpActivityMapper;
    @Autowired
    private TaShareActivityMapper taShareActivityMapper;
    @Autowired
    private TaDrainageMapper taDrainageMapper;

    @Override
    public ResponseBean extendContentAdd(ExtendContent extendContent) {
        ResponseBean responseBean= new ResponseBean<>();
        if (extendContent.getStatus() == null){
            extendContent.setStatus(0);
        }
        extendContentMapper.insert(extendContent);
        responseBean.addSuccess("成功");
        return  responseBean;
    }

    @Override
    @Deprecated
    public ResponseBean selectExtendContentList(String contentType, String buildingId) {
        ResponseBean responseBean = new ResponseBean<>();
        // 查询轮播图
        QueryWrapper<ExtendContent> extendContentQuery= new QueryWrapper<>();
        // banner 或 screen(首屏广告) ,这里是 banner
        extendContentQuery.eq("show_position",CommConstant.CAROUSEL);
        // activity(活动) 或 project(项目) 或 news（咨迅）
        extendContentQuery.eq("content_type",contentType);
        extendContentQuery.eq("building_id",buildingId);
        extendContentQuery.eq("status","1");
        List<ExtendContent> taExtendContent = extendContentMapper.selectList(extendContentQuery);
        responseBean.addSuccess(taExtendContent);
        return responseBean;
    }

    @Override
    public ResponseBean selectExtendContentListShowType(String showType, String showPosition, String targetId, String contentType, String buildingId, String contentId, Integer cityId,Integer orgId) {
        ResponseBean responseBean= new ResponseBean<>();
        // 开屏
        QueryWrapper<ExtendContent> extendContentQuery= new QueryWrapper<>();
        // banner 或 screen(首屏广告) ,这里是 screen
        extendContentQuery.eq(StringUtils.isNotBlank(showType), "show_type",showType);
        extendContentQuery.eq(StringUtils.isNotBlank(showPosition), "show_position",showPosition);
        extendContentQuery.eq(StringUtils.isNotBlank(targetId), "target_id",targetId);
        extendContentQuery.eq(StringUtils.isNotBlank(contentId), "content_id",contentId);
        // activity(活动) 或 project(项目) 或 news（咨迅）
        extendContentQuery.eq(StringUtils.isNotBlank(contentType), "content_type",contentType);
        extendContentQuery.eq(StringUtils.isNotBlank(buildingId), "building_id",buildingId);
        extendContentQuery.eq(null != cityId && cityId != 0, "city_id", cityId);
        extendContentQuery.eq("status","1");
        extendContentQuery.eq("org_id",orgId);
        extendContentQuery.orderByDesc("order_no");
        extendContentQuery.orderByDesc("create_date");
        List<ExtendContent> taExtendContent = extendContentMapper.selectList(extendContentQuery);
        responseBean.addSuccess(taExtendContent);
        return responseBean;
    }

    @Override
    public void updateActivity(String dynamicId, String type,Integer orgId) {
    QueryWrapper<ExtendContent> queryWrapperExtendContent = new QueryWrapper<>();
        queryWrapperExtendContent.eq("target_id",dynamicId);
        queryWrapperExtendContent.eq("content_type",type);
        queryWrapperExtendContent.eq("org_id",orgId);
        List<ExtendContent> list = extendContentMapper.selectList(queryWrapperExtendContent);
        for (ExtendContent extendContent : list){
            extendContent.setStatus(0);
            extendContentMapper.updateById(extendContent);
        }
    }

    @Override
    public boolean judgleRelationBuildAndActivity(ExtendContent extendContent) {
        //判断已关联的项目或者活动是否取消发布
        if (StringUtils.isNotEmpty(extendContent.getBuildingId())){
            QueryWrapper<TaBuilding> taBuildingQueryWrapper = new QueryWrapper<>();
            taBuildingQueryWrapper.eq("building_id", extendContent.getBuildingId());
            TaBuilding taBuilding = taBuildingMapper.selectOne(taBuildingQueryWrapper);
            if (taBuilding.getStatus() == 0){
                return false;
            }
        }

        if (CommConstant.CAROUSEL_ACTIVITY.equals(extendContent.getContentType())
                && StringUtils.isNotEmpty(extendContent.getTargetId())){
            QueryWrapper<TaBuildingDynamic> taBuildingDynamicQueryWrapper = new QueryWrapper<>();
            taBuildingDynamicQueryWrapper.eq("dynamic_id", extendContent.getTargetId());
            TaBuildingDynamic taBuildingDynamic = taBuildingDynamicMapper.selectOne(taBuildingDynamicQueryWrapper);
            if (taBuildingDynamic.getStatus() == 0){
                return false;
            }
        }

        if (CommConstant.CAROUSEL_NEWS.equals(extendContent.getContentType())
                && StringUtils.isNotEmpty(extendContent.getTargetId())){
            QueryWrapper<TaNews> taNewsQueryWrapper = new QueryWrapper<>();
            taNewsQueryWrapper.eq("news_id", extendContent.getTargetId());
            TaNews taNews = taNewsMapper.selectOne(taNewsQueryWrapper);
            if (taNews.getNewsStatus() == 1){
                return false;
            }
        }

        if (CommConstant.POINTS_CHANGE_HELP.equals(extendContent.getContentType())
                && StringUtils.isNotEmpty(extendContent.getTargetId())){
            QueryWrapper<HelpActivity> helpActivityQueryWrapper = new QueryWrapper<>();
            helpActivityQueryWrapper.eq("help_activity_id", extendContent.getTargetId());
            HelpActivity helpActivity = helpActivityMapper.selectOne(helpActivityQueryWrapper);
            if (helpActivity.getStatus() == -1){
                return false;
            }
        }

        if (CommConstant.POINTS_CHANGE_GROUP.equals(extendContent.getContentType())
                && StringUtils.isNotEmpty(extendContent.getTargetId())){
            QueryWrapper<TaShareActivity> taShareActivityQueryWrapper = new QueryWrapper<>();
            taShareActivityQueryWrapper.eq("group_activity_id", extendContent.getTargetId());
            TaShareActivity taShareActivity = taShareActivityMapper.selectOne(taShareActivityQueryWrapper);
            if (taShareActivity.getStatus() == -1){
                return false;
            }
        }

        if (CommConstant.TARGET_TYPE_H5.equals(extendContent.getContentType())
                && StringUtils.isNotEmpty(extendContent.getTargetId())){
            QueryWrapper<TaDrainage> taDrainageQueryWrapper = new QueryWrapper<>();
            taDrainageQueryWrapper.eq("drainage_id", extendContent.getTargetId());
            TaDrainage taDrainage = taDrainageMapper.selectOne(taDrainageQueryWrapper);
            if (taDrainage.getStatus() == 0){
                return false;
            }
        }


        return true;
    }

    @Override
    public boolean advertistExistFlag(ExtendContent extendContent, Integer orgId) {
        //一个城市、项目只能有一个开屏广告
        if (extendContent.getStatus() == 1 && CommConstant.OPEN_SCREEN.equals(extendContent.getShowType())){
            QueryWrapper<ExtendContent> taExtendContentQueryWrapper = new QueryWrapper<>();
            if (extendContent.getCityId() == null){
                TaBuilding taBuilding = taBuildingMapper.selectBuilding(extendContent.getBuildingId());
                taExtendContentQueryWrapper.eq("city_id", taBuilding.getCityId());
            }else{
                taExtendContentQueryWrapper.eq("city_id", extendContent.getCityId());
            }

            taExtendContentQueryWrapper.eq("org_id", orgId);
            taExtendContentQueryWrapper.eq("status", 1);
            taExtendContentQueryWrapper.eq("show_type", extendContent.getShowType());
            taExtendContentQueryWrapper.eq("show_position", extendContent.getShowPosition());
            if (null != extendContent.getContentId()){
                taExtendContentQueryWrapper.ne("content_id", extendContent.getContentId());
            }
//            taExtendContentQueryWrapper.eq(StringUtils.isNotBlank(String.valueOf(extendContent.getIsHaveActive())), "is_have_active", extendContent.getIsHaveActive());
            if (null != extendContent.getIsHaveActive() && CommConstant.BANNER_BUILDING.equals(extendContent.getShowPosition())){
                taExtendContentQueryWrapper.eq(StringUtils.isNotBlank(extendContent.getBuildingId()), "building_id", extendContent.getBuildingId());
            }
            List<ExtendContent> extContentInfo = extendContentMapper.selectList(taExtendContentQueryWrapper);
            if (!Collections.isEmpty(extContentInfo)){
                return false;
            }
        }
        return true;
    }
}
