package org.dromara.elevator.service.impl;

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.EElevatorUser;
import org.dromara.elevator.domain.EWorkData;
import org.dromara.elevator.domain.vo.EElevatorInfoVo;
import org.dromara.elevator.mapper.EElevatorInfoMapper;
import org.dromara.elevator.mapper.EElevatorUserMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EWorkerInfoBo;
import org.dromara.elevator.domain.vo.EWorkerInfoVo;
import org.dromara.elevator.domain.EWorkerInfo;
import org.dromara.elevator.mapper.EWorkerInfoMapper;
import org.dromara.elevator.service.IEWorkerInfoService;

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

/**
 * 工作人员管理;Service业务层处理
 *
 * @author zy
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class EWorkerInfoServiceImpl implements IEWorkerInfoService {

    private final EWorkerInfoMapper baseMapper;
    private final EElevatorUserMapper elevatorUserMapper;
    private final EElevatorInfoMapper elevatorInfoMapper;

    /**
     * 查询工作人员管理;
     *
     * @param id 主键
     * @return 工作人员管理;
     */
    @Override
    public EWorkerInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询工作人员管理;列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 工作人员管理;分页列表
     */
    @Override
    public TableDataInfo<EWorkerInfoVo> queryPageList(EWorkerInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EWorkerInfo> lqw = buildQueryWrapper(bo);
        Page<EWorkerInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的工作人员管理;列表
     *
     * @param bo 查询条件
     * @return 工作人员管理;列表
     */
    @Override
    public List<EWorkerInfoVo> queryList(EWorkerInfoBo bo) {
        LambdaQueryWrapper<EWorkerInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EWorkerInfo> buildQueryWrapper(EWorkerInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkerInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUnitType() != null, EWorkerInfo::getUnitType, bo.getUnitType());
        lqw.eq(StringUtils.isNotBlank(bo.getRelevancyUnit()), EWorkerInfo::getRelevancyUnit, bo.getRelevancyUnit());
        lqw.eq(bo.getEmployeeType() != null, EWorkerInfo::getEmployeeType, bo.getEmployeeType());
        lqw.eq(StringUtils.isNotBlank(bo.getRelevancyProject()), EWorkerInfo::getRelevancyProject, bo.getRelevancyProject());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), EWorkerInfo::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getPhoneName()), EWorkerInfo::getPhoneName, bo.getPhoneName());
        lqw.eq(StringUtils.isNotBlank(bo.getVoicePhone()), EWorkerInfo::getVoicePhone, bo.getVoicePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultPhone()), EWorkerInfo::getFaultPhone, bo.getFaultPhone());
        lqw.eq(bo.getIsFault() != null, EWorkerInfo::getIsFault, bo.getIsFault());
        lqw.eq(bo.getIsVoice() != null, EWorkerInfo::getIsVoice, bo.getIsVoice());
        lqw.eq(bo.getIsBehaviorRemind() != null, EWorkerInfo::getIsBehaviorRemind, bo.getIsBehaviorRemind());
        lqw.eq(bo.getIsDisturb() != null, EWorkerInfo::getIsDisturb, bo.getIsDisturb());
        lqw.eq(bo.getIsAllElevator() != null, EWorkerInfo::getIsAllElevator, bo.getIsAllElevator());
        lqw.eq(StringUtils.isNotBlank(bo.getIdNumber()), EWorkerInfo::getIdNumber, bo.getIdNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCredential()), EWorkerInfo::getCredential, bo.getCredential());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), EWorkerInfo::getPhoto, bo.getPhoto());
        lqw.orderByDesc(EWorkerInfo::getCreateTime);
        return lqw;
    }

    /**
     * 新增工作人员管理;
     *
     * @param bo 工作人员管理;
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EWorkerInfoBo bo) {
        EWorkerInfo add = MapstructUtils.convert(bo, EWorkerInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        // 插入工作人员和电梯关系数据
//        List<EElevatorUser> userList = new ArrayList<>();
//        List<EElevatorInfoVo> elevatorList = elevatorInfoMapper.selectVoList();
//        for (EElevatorInfoVo elevator : elevatorList) {
//            EElevatorUser elevatorUser = new EElevatorUser();
//            elevatorUser.setElevatorId(String.valueOf(elevator.getId()));
//            elevatorUser.setElevatorNum(elevator.getElevatorNum());
//            elevatorUser.setElevatorName(elevator.getElevatorName());
//            elevatorUser.setElevatorAddress(elevator.getElevatorAddress());
//            elevatorUser.setTenementName(elevator.getTenementName());
//            elevatorUser.setMaintenanceName(elevator.getMaintenanceName());
//            elevatorUser.setUserId(String.valueOf(add.getId()));
//            elevatorUser.setIsConcern(0);
//            userList.add(elevatorUser);
//        }
//        elevatorUserMapper.insertBatch(userList);

        return flag;
    }

    /**
     * 修改工作人员管理;
     *
     * @param bo 工作人员管理;
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EWorkerInfoBo bo) {
        EWorkerInfo update = MapstructUtils.convert(bo, EWorkerInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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