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.*;
import com.huiju.estateagents.entity.TpNewsType;
import com.huiju.estateagents.exception.EstaException;
import com.huiju.estateagents.mapper.TpNewsTypeMapper;
import com.huiju.estateagents.mapper.TpNewsTypeMapper;
import com.huiju.estateagents.service.*;
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 TpNewsTypeServiceImpl extends ServiceImpl<TpNewsTypeMapper, TpNewsType> implements ITpNewsTypeService {

    @Autowired
    private ITaBuildingService iTaBuildingService;

    @Autowired
    private ITpNewsService iTpNewsService;


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

        QueryWrapper<TpNewsType> 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<TpNewsType> typeIPage = this.page(page, taNewsQueryWrapper);

        responseBean.addSuccess(typeIPage);
        return responseBean;
    }

    @Override
    public ResponseBean getTpNewsTypeById(Integer newsTypeId) {
        ResponseBean responseBean = new ResponseBean();
        if (null == newsTypeId) {
            responseBean.addError("物业服务类型编号不存在!");
        }
        responseBean.addSuccess(this.getById(newsTypeId));
        return responseBean;
    }

    @Override
    public ResponseBean updateTpNewsType(TpNewsType TpNewsType) {
        ResponseBean responseBean =  new ResponseBean();
        TpNewsType oldTpNewsType = this.getById(TpNewsType.getNewsTypeId());
        if (null == oldTpNewsType) {
            responseBean.addError("物业服务类型不存在！");
            return responseBean;
        }

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

        BeanTools.copyProperties(TpNewsType, oldTpNewsType);
        this.updateById(oldTpNewsType);
        responseBean.addSuccess(oldTpNewsType);
        return responseBean;
    }

    @Override
    public ResponseBean addTpNewsType(TpNewsType TpNewsType, Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

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

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

    private void check(TpNewsType TpNewsType) {
//        TaBuilding taBuilding = iTaBuildingService.getById(TpNewsType.getBuildingId());
//        if (null == taBuilding) {
//            throw new EstaException("楼盘不存在！");
//        }
        if (TpNewsType.getStatus() != null && TpNewsType.getStatus() == -1) {
            QueryWrapper<TpNews> queryWrapper = new QueryWrapper<>();
            queryWrapper.gt("status", -1);
            queryWrapper.eq("news_type_id", TpNewsType.getNewsTypeId());
            Integer newsCount = iTpNewsService.count(queryWrapper);
            if (newsCount != null && newsCount > 0) {
                throw new EstaException("有物业服务正在使用此物业服务！");
            }
        }

    }

    @Override
    public ResponseBean deleteTpNewsType(Integer newsTypeId) {
        ResponseBean responseBean = getTpNewsTypeById(newsTypeId);
        if (1 == responseBean.getCode()) {
            return responseBean;
        }
        TpNewsType TpNewsType = (TpNewsType) responseBean.getData();
        if (null == TpNewsType) {
            responseBean.addError("数据不存!");
        }

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

        responseBean.addSuccess(TpNewsType);
        return responseBean;
    }

}
