package org.dromara.elevator.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.elevator.domain.EElevatorInfo;
import org.dromara.elevator.domain.ETenementInfo;
import org.dromara.elevator.mapper.EElevatorInfoMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EThirdPartyUserBo;
import org.dromara.elevator.domain.vo.EThirdPartyUserVo;
import org.dromara.elevator.domain.EThirdPartyUser;
import org.dromara.elevator.mapper.EThirdPartyUserMapper;
import org.dromara.elevator.service.IEThirdPartyUserService;

import java.util.*;

/**
 * 物业维保等用户信息Service业务层处理
 *
 * @author zy
 * @date 2024-09-04
 */
@RequiredArgsConstructor
@Service
public class EThirdPartyUserServiceImpl implements IEThirdPartyUserService {

    private final EThirdPartyUserMapper baseMapper;
    private final EElevatorInfoMapper elevatorInfoMapper;

    /**
     * 查询物业维保等用户信息
     *
     * @param id 主键
     * @return 物业维保等用户信息
     */
    @Override
    public EThirdPartyUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询物业维保等用户信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 物业维保等用户信息分页列表
     */
    @Override
    public TableDataInfo<EThirdPartyUserVo> queryPageList(EThirdPartyUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EThirdPartyUser> lqw = buildQueryWrapper(bo);
        Page<EThirdPartyUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的物业维保等用户信息列表
     *
     * @param bo 查询条件
     * @return 物业维保等用户信息列表
     */
    @Override
    public List<EThirdPartyUserVo> queryList(EThirdPartyUserBo bo) {
        LambdaQueryWrapper<EThirdPartyUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EThirdPartyUser> buildQueryWrapper(EThirdPartyUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EThirdPartyUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), EThirdPartyUser::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitCode()), EThirdPartyUser::getUnitCode, bo.getUnitCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), EThirdPartyUser::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), EThirdPartyUser::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), EThirdPartyUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getMail()), EThirdPartyUser::getMail, bo.getMail());
        lqw.eq(bo.getIsAdmin() != null, EThirdPartyUser::getIsAdmin, bo.getIsAdmin());
        lqw.in(CollectionUtils.isNotEmpty(bo.getUnitIds()), EThirdPartyUser::getUnitId, bo.getUnitIds());

        lqw.orderByDesc(EThirdPartyUser::getCreateTime);
        return lqw;
    }

    /**
     * 新增物业维保等用户信息
     *
     * @param bo 物业维保等用户信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EThirdPartyUserBo bo) {
        EThirdPartyUser add = MapstructUtils.convert(bo, EThirdPartyUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物业维保等用户信息
     *
     * @param bo 物业维保等用户信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EThirdPartyUserBo bo) {
        EThirdPartyUser update = MapstructUtils.convert(bo, EThirdPartyUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 根据电梯编号查询物业维保等人员信息
     * @param elevatorNum
     * @return
     */
    @Override
    public List<Map<String, Object>> getThirdPartyUser(String elevatorNum) {
        List<Map<String, Object>> returnList = new ArrayList<>();
        // 查询电梯基础信息
        EElevatorInfo elevatorInfo = elevatorInfoMapper.selectOne(Wrappers.<EElevatorInfo>lambdaQuery().eq(EElevatorInfo::getElevatorNum, elevatorNum));
        if (Objects.isNull(elevatorInfo)) {
            throw new ServiceException("未查询到该电梯信息!");
        }
        // 获取物业单位人员
        List<EThirdPartyUserVo> tenementList = baseMapper.selectVoList(Wrappers.<EThirdPartyUser>lambdaQuery().eq(EThirdPartyUser::getUnitId, elevatorInfo.getTenementId()));
        Map<String, Object> tenementMap = new HashMap<>();
        tenementMap.put("tenementName", elevatorInfo.getTenementName());
        tenementMap.put("tenementList", tenementList);
        returnList.add(tenementMap);
        // 获取维保单位人员
        List<EThirdPartyUserVo> maintenanceList = baseMapper.selectVoList(Wrappers.<EThirdPartyUser>lambdaQuery().eq(EThirdPartyUser::getUnitId, elevatorInfo.getMaintenanceId()));
        Map<String, Object> maintenanceMap = new HashMap<>();
        maintenanceMap.put("maintenanceName", elevatorInfo.getMaintenanceName());
        maintenanceMap.put("maintenanceList", maintenanceList);
        returnList.add(maintenanceMap);

        return returnList;
    }
}
