package com.huiju.estateagents.service.impl;

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 com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.BeanTools;
import com.huiju.estateagents.entity.TaBuilding;
import com.huiju.estateagents.entity.TaNews;
import com.huiju.estateagents.entity.TaNewsType;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.exception.EstaException;
import com.huiju.estateagents.mapper.TaNewsTypeMapper;
import com.huiju.estateagents.service.ITaBuildingService;
import com.huiju.estateagents.service.ITaNewsService;
import com.huiju.estateagents.service.ITaNewsTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资讯类型表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@Service
public class TaNewsTypeServiceImpl extends ServiceImpl<TaNewsTypeMapper, TaNewsType> implements ITaNewsTypeService {

    @Autowired
    private ITaBuildingService iTaBuildingService;

    @Autowired
    private ITaNewsService iTaNewsService;


    @Override
    public ResponseBean getList(Integer pageNum, Integer pageSize, String buildingId, Integer orgId, List<TaPersonBuilding> personBuildingList) {
        ResponseBean responseBean = new ResponseBean();
        IPage<TaNewsType> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<TaNewsType> taNewsQueryWrapper = new QueryWrapper<>();
        taNewsQueryWrapper.eq(StringUtils.isNotBlank(buildingId),"building_id", buildingId);
        taNewsQueryWrapper.eq(orgId != null,"org_id", orgId);
        taNewsQueryWrapper.eq("status", 1);
        taNewsQueryWrapper.in(personBuildingList.size() > 0,"building_id", personBuildingList.stream().map(TaPersonBuilding::getBuildingId).collect(Collectors.toList()));
        taNewsQueryWrapper.orderByDesc("create_date");
        IPage<TaNewsType> typeIPage = this.page(page, taNewsQueryWrapper);

        responseBean.addSuccess(typeIPage);
        return responseBean;
    }

    @Override
    public ResponseBean getTaNewsTypeById(Integer newsTypeId) {
        ResponseBean responseBean = new ResponseBean();
        if (null == newsTypeId) {
            responseBean.addError("资讯类型编号不存在!");
        }
        responseBean.addSuccess(this.getById(newsTypeId));
        return responseBean;
    }

    @Override
    public ResponseBean updateTaNewsType(TaNewsType taNewsType) {
        ResponseBean responseBean =  new ResponseBean();
        TaNewsType oldTaNewsType = this.getById(taNewsType.getNewsTypeId());
        if (null == oldTaNewsType) {
            responseBean.addError("资讯类型不存在！");
            return responseBean;
        }

        //-- 校验 公司，楼盘
        // TODO 没有校验公司  taNewsType.getOrgId();
        check(taNewsType);

        BeanTools.copyProperties(taNewsType, oldTaNewsType);
        this.updateById(oldTaNewsType);
        responseBean.addSuccess(oldTaNewsType);
        return responseBean;
    }

    @Override
    public ResponseBean addTaNewsType(TaNewsType taNewsType, Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        //-- 校验 公司，楼盘
        check(taNewsType);

        taNewsType.setCreateDate(LocalDateTime.now());
        taNewsType.setStatus(1);
        taNewsType.setOrgId(orgId);
        this.save(taNewsType);
        responseBean.addSuccess(taNewsType);
        return responseBean;
    }

    private void check(TaNewsType taNewsType) {
//        TaBuilding taBuilding = iTaBuildingService.getById(taNewsType.getBuildingId());
//        if (null == taBuilding) {
//            throw new EstaException("楼盘不存在！");
//        }
        if (taNewsType.getStatus() != null && taNewsType.getStatus() == -1) {
            QueryWrapper<TaNews> queryWrapper = new QueryWrapper<>();
            queryWrapper.gt("status", -1);
            queryWrapper.eq("news_type_id", taNewsType.getNewsTypeId());
            Integer newsCount = iTaNewsService.count(queryWrapper);
            if (newsCount != null && newsCount > 0) {
                throw new EstaException("有资讯正在使用此资讯类型！");
            }
        }

    }

    @Override
    public ResponseBean deleteTaNewsType(Integer newsTypeId) {
        ResponseBean responseBean = getTaNewsTypeById(newsTypeId);
        if (1 == responseBean.getCode()) {
            return responseBean;
        }
        TaNewsType taNewsType = (TaNewsType) responseBean.getData();
        if (null == taNewsType) {
            responseBean.addError("数据不存!");
        }

        // 修改状态
        taNewsType.setStatus(0);
        this.updateById(taNewsType);

        responseBean.addSuccess(taNewsType);
        return responseBean;
    }

}
