package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.domain.visitor.VisitorManage;
import org.dromara.domain.visitor.bo.VisitorManageBo;
import org.dromara.domain.visitor.vo.VisitorManageVo;
import org.dromara.mapper.VisitorManageMapper;
import org.dromara.service.IVisitorManageService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.dromara.common.core.constant.Constants.NOW_MONTH_VISITOR_CAR;
import static org.dromara.common.core.constant.Constants.Y;

/**
 * 来访管理Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class VisitorManageServiceImpl implements IVisitorManageService {

    private final VisitorManageMapper baseMapper;

    /**
     * 查询来访管理
     *
     * @param visitorManageId 主键
     * @return 来访管理
     */
    @Override
    public VisitorManageVo queryById(Long visitorManageId){
        return baseMapper.selectVoById(visitorManageId);
    }

    /**
     * 分页查询来访管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 来访管理分页列表
     */
    @Override
    public TableDataInfo<VisitorManageVo> queryPageList(VisitorManageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VisitorManage> lqw = buildQueryWrapper(bo);
        Page<VisitorManageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的来访管理列表
     *
     * @param bo 查询条件
     * @return 来访管理列表
     */
    @Override
    public List<VisitorManageVo> queryList(VisitorManageBo bo) {
        LambdaQueryWrapper<VisitorManage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VisitorManage> buildQueryWrapper(VisitorManageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VisitorManage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(VisitorManage::getVisitorManageId);
        lqw.eq(bo.getPortalId() != null, VisitorManage::getPortalId, bo.getPortalId());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseAddress()), VisitorManage::getHouseAddress, bo.getHouseAddress());
        lqw.like(StringUtils.isNotBlank(bo.getName()), VisitorManage::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getNum()), VisitorManage::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), VisitorManage::getReason, bo.getReason());
        lqw.eq(StringUtils.isNotBlank(bo.getMobileNumber()), VisitorManage::getMobileNumber, bo.getMobileNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCar()), VisitorManage::getIsCar, bo.getIsCar());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), VisitorManage::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getReserveTime() != null, VisitorManage::getReserveTime, bo.getReserveTime());
        lqw.eq(bo.getVisitorTime() != null, VisitorManage::getVisitorTime, bo.getVisitorTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), VisitorManage::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), VisitorManage::getPhoto, bo.getPhoto());
        return lqw;
    }

    /**
     * 新增来访管理
     *
     * @param bo 来访管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(VisitorManageBo bo) {
        VisitorManage visitorManage = MapstructUtils.convert(bo, VisitorManage.class);
        validEntityBeforeSave(visitorManage);
        visitorManage.setVisitorTime(DateUtils.getNowDate());
        //统计中外来车辆
        if (Y.equals(visitorManage.getIsCar())) {
            int currentMonth = 1;
            Object nowMonthRedis = RedisUtils.getCacheObject(NOW_MONTH_VISITOR_CAR + DateUtils.getMonth());
            if (ObjectUtils.isNotEmpty(nowMonthRedis)) {
                currentMonth = (int) nowMonthRedis + currentMonth;
            }
            RedisUtils.setCacheObject(NOW_MONTH_VISITOR_CAR + DateUtils.getMonth(), currentMonth);
        }

        boolean flag = baseMapper.insert(visitorManage) > 0;
        if (flag) {
            bo.setVisitorManageId(visitorManage.getVisitorManageId());
        }
        return flag;
    }

    /**
     * 修改来访管理
     *
     * @param bo 来访管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(VisitorManageBo bo) {
        VisitorManage visitorManage = MapstructUtils.convert(bo, VisitorManage.class);
        validEntityBeforeSave(visitorManage);
        if (Y.equals(visitorManage.getIsCar())) {
            VisitorManage visitorManageOld = baseMapper.selectById(visitorManage.getVisitorManageId());
            if (!Y.equals(visitorManageOld.getIsCar())) {
                int currentMonth = 1;
                Object nowMonthRedis = RedisUtils.getCacheObject(NOW_MONTH_VISITOR_CAR + DateUtils.getMonth());
                if (ObjectUtils.isNotEmpty(nowMonthRedis)) {
                    currentMonth = (int) nowMonthRedis + currentMonth;
                }
                RedisUtils.setCacheObject(NOW_MONTH_VISITOR_CAR + DateUtils.getMonth(), currentMonth);
            }
        }

        return baseMapper.updateById(visitorManage) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VisitorManage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除来访管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
