package fcb.project.manager.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fcb.project.manager.base.dao.TmEstateDao;
import fcb.project.manager.base.entity.ContextTokenBean;
import fcb.project.manager.base.entity.TmEstate;
import fcb.project.manager.base.enums.DeleteStatus;
import fcb.project.manager.base.enums.SysUserRoleEnums;
import fcb.project.manager.base.service.ITmEstateService;
import fdage.back.sdk.base.uuid.SnowFlakeUUidUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author abner
 * @since 2021-01-07
 */
@Service
@Log4j2
public class TmEstateServiceImpl extends ServiceImpl<TmEstateDao, TmEstate> implements ITmEstateService {



    public IPage<TmEstate> getListByNameOrBelong(String estateName , String belongCompany, ContextTokenBean contextTokenBean,
                                                 Long pageNum , Long pageSize){
        LambdaQueryWrapper<TmEstate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(belongCompany)){
            lambdaQueryWrapper.eq(TmEstate::getBelongCompanyId , belongCompany);
        }
        if(StringUtils.isNotBlank(estateName)){
            lambdaQueryWrapper.like(TmEstate::getEstateName , estateName);
        }
        if(null != contextTokenBean){
            if (StringUtils.equals(contextTokenBean.getRoleKey(), SysUserRoleEnums.ADMIN.getRoleKey())
                    || StringUtils.equals(contextTokenBean.getRoleKey(), SysUserRoleEnums.GROUP.getRoleKey())) {
                //超级管理员和集团管理员可以看到所有数据

            } else {
                lambdaQueryWrapper.eq(TmEstate::getBelongCompanyId , contextTokenBean.getDepartmentId());
            }

        }
        lambdaQueryWrapper.eq(TmEstate::getIsDelete , DeleteStatus.NOT_DELETE.getCode());
        lambdaQueryWrapper.orderByDesc(TmEstate::getCreateTime);
        IPage<TmEstate> page = new Page<>(pageNum , pageSize);
        return  getBaseMapper().selectPage(page , lambdaQueryWrapper);
    }

    public TmEstate getOneByNameAndBelong(String estateName , String belongCompany){
        LambdaQueryWrapper<TmEstate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(estateName)){
            lambdaQueryWrapper.eq(TmEstate::getBelongCompanyName , belongCompany);
        }
        if(StringUtils.isNotBlank(estateName)){
            lambdaQueryWrapper.eq(TmEstate::getEstateName , estateName);
        }
        lambdaQueryWrapper.eq(TmEstate::getIsDelete , DeleteStatus.NOT_DELETE.getCode());
        lambdaQueryWrapper.last("limit 1");
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    public int deleteEstateById(String estateId){
        if(StringUtils.isBlank(estateId)){
            return -1;
        }
        return getBaseMapper().deleteById(estateId);
    }


    public boolean updateEstate(TmEstate estate){
        if(null == estate){
            return false;
        }
        if(null == estate.getId()){
            log.info("主键ID为空，无法更项目楼盘信息");
            return false;
        }
        estate.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(estate);
    }

    public boolean insertNew(TmEstate estate){
        if(StringUtils.isBlank(estate.getId())){
            estate.setId(SnowFlakeUUidUtils.generaUUid(null , null ,  "EST00001"));
        }
        estate.setCreateTime(LocalDateTime.now());
        estate.setUpdateTime(LocalDateTime.now());
        estate.setIsDelete(DeleteStatus.NOT_DELETE.getCode());
        return 1 == getBaseMapper().insert(estate);
    }

}
