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.EElevatorInfo;
import org.dromara.elevator.mapper.EElevatorInfoMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EPassengerFeedbackBo;
import org.dromara.elevator.domain.vo.EPassengerFeedbackVo;
import org.dromara.elevator.domain.EPassengerFeedback;
import org.dromara.elevator.mapper.EPassengerFeedbackMapper;
import org.dromara.elevator.service.IEPassengerFeedbackService;

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

/**
 * 乘客反馈信息;Service业务层处理
 *
 * @author zy
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class EPassengerFeedbackServiceImpl implements IEPassengerFeedbackService {

    private final EPassengerFeedbackMapper baseMapper;
    private final EElevatorInfoMapper elevatorInfoMapper;

    /**
     * 查询乘客反馈信息;
     *
     * @param id 主键
     * @return 乘客反馈信息;
     */
    @Override
    public EPassengerFeedbackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询乘客反馈信息;列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 乘客反馈信息;分页列表
     */
    @Override
    public TableDataInfo<EPassengerFeedbackVo> queryPageList(EPassengerFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EPassengerFeedback> lqw = buildQueryWrapper(bo);
        Page<EPassengerFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的乘客反馈信息;列表
     *
     * @param bo 查询条件
     * @return 乘客反馈信息;列表
     */
    @Override
    public List<EPassengerFeedbackVo> queryList(EPassengerFeedbackBo bo) {
        LambdaQueryWrapper<EPassengerFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EPassengerFeedback> buildQueryWrapper(EPassengerFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EPassengerFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EPassengerFeedback::getElevatorNum, bo.getElevatorNum());
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), EPassengerFeedback::getElevatorName, bo.getElevatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorAddress()), EPassengerFeedback::getElevatorAddress, bo.getElevatorAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), EPassengerFeedback::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsPhone()), EPassengerFeedback::getContactsPhone, bo.getContactsPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getTenement()), EPassengerFeedback::getTenement, bo.getTenement());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintenance()), EPassengerFeedback::getMaintenance, bo.getMaintenance());
        lqw.eq(StringUtils.isNotBlank(bo.getBrand()), EPassengerFeedback::getBrand, bo.getBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getBuilding()), EPassengerFeedback::getBuilding, bo.getBuilding());
        lqw.eq(Objects.nonNull(bo.getReplyState()), EPassengerFeedback::getReplyState, bo.getReplyState());
        lqw.eq(bo.getReplyTime() != null, EPassengerFeedback::getReplyTime, bo.getReplyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), EPassengerFeedback::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getSuggestion()), EPassengerFeedback::getSuggestion, bo.getSuggestion());
        lqw.orderByDesc(EPassengerFeedback::getCreateTime);
        return lqw;
    }

    /**
     * 新增乘客反馈信息;
     *
     * @param bo 乘客反馈信息;
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EPassengerFeedbackBo bo) {
        EPassengerFeedback add = MapstructUtils.convert(bo, EPassengerFeedback.class);
        validEntityBeforeSave(add);
        EElevatorInfo elevatorInfo = elevatorInfoMapper.selectOne(Wrappers.<EElevatorInfo>lambdaQuery()
            .eq(EElevatorInfo::getElevatorNum, bo.getElevatorNum())
            .last(" limit 1"));
        if (Objects.nonNull(elevatorInfo) && Objects.nonNull(add)) {
            add.setElevatorName(elevatorInfo.getElevatorName());
            add.setElevatorAddress(elevatorInfo.getElevatorAddress());
            add.setTenement(elevatorInfo.getTenementName());
            add.setMaintenance(elevatorInfo.getMaintenanceName());
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改乘客反馈信息;
     *
     * @param bo 乘客反馈信息;
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EPassengerFeedbackBo bo) {
        EPassengerFeedback update = MapstructUtils.convert(bo, EPassengerFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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