package org.dromara.elevator.service.impl;

import org.apache.commons.collections4.CollectionUtils;
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.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EWorkOperationRecordBo;
import org.dromara.elevator.domain.vo.EWorkOperationRecordVo;
import org.dromara.elevator.domain.EWorkOperationRecord;
import org.dromara.elevator.mapper.EWorkOperationRecordMapper;
import org.dromara.elevator.service.IEWorkOperationRecordService;

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

/**
 * 工作操作记录Service业务层处理
 *
 * @author ym
 * @date 2024-09-11
 */
@RequiredArgsConstructor
@Service
public class EWorkOperationRecordServiceImpl implements IEWorkOperationRecordService {

    private final EWorkOperationRecordMapper baseMapper;

    /**
     * 查询工作操作记录
     *
     * @param id 主键
     * @return 工作操作记录
     */
    @Override
    public EWorkOperationRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询工作操作记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 工作操作记录分页列表
     */
    @Override
    public TableDataInfo<EWorkOperationRecordVo> queryPageList(EWorkOperationRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EWorkOperationRecord> lqw = buildQueryWrapper(bo);
        Page<EWorkOperationRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的工作操作记录列表
     *
     * @param bo 查询条件
     * @return 工作操作记录列表
     */
    @Override
    public List<EWorkOperationRecordVo> queryList(EWorkOperationRecordBo bo) {
        LambdaQueryWrapper<EWorkOperationRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EWorkOperationRecord> buildQueryWrapper(EWorkOperationRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EWorkOperationRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), EWorkOperationRecord::getOperator, bo.getOperator());
        lqw.eq(bo.getOperationTime() != null, EWorkOperationRecord::getOperationTime, bo.getOperationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EWorkOperationRecord::getElevatorNum, bo.getElevatorNum());
        lqw.eq(bo.getWorkId() != null, EWorkOperationRecord::getWorkId, bo.getWorkId());
        lqw.eq(bo.getWorkType() != null, EWorkOperationRecord::getWorkType, bo.getWorkType());
        lqw.eq(bo.getOperationState() != null, EWorkOperationRecord::getOperationState, bo.getOperationState());
        lqw.eq(bo.getExpectTime() != null, EWorkOperationRecord::getExpectTime, bo.getExpectTime());
        lqw.eq(bo.getLng() != null, EWorkOperationRecord::getLng, bo.getLng());
        lqw.eq(bo.getLat() != null, EWorkOperationRecord::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EWorkOperationRecord::getAddress, bo.getAddress());
        lqw.eq(bo.getMaintenanceId() != null, EWorkOperationRecord::getMaintenanceId, bo.getMaintenanceId());
        lqw.eq(bo.getMaintenancePlanId() != null, EWorkOperationRecord::getMaintenancePlanId, bo.getMaintenancePlanId());
        lqw.eq(bo.getTenementId() != null, EWorkOperationRecord::getTenementId, bo.getTenementId());
        lqw.eq(bo.getOperatorId() != null, EWorkOperationRecord::getOperatorId, bo.getOperatorId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationContent()), EWorkOperationRecord::getOperationContent, bo.getOperationContent());
        lqw.eq(bo.getDeptId() != null, EWorkOperationRecord::getDeptId, bo.getDeptId());

        // 统计查询字段条件
        lqw.in(CollectionUtils.isNotEmpty(bo.getElevatorNumList()), EWorkOperationRecord::getElevatorNum, bo.getElevatorNumList());
        lqw.in(CollectionUtils.isNotEmpty(bo.getMaintenanceIds()), EWorkOperationRecord::getMaintenanceId, bo.getMaintenanceIds());
        lqw.in(CollectionUtils.isNotEmpty(bo.getTenementIds()), EWorkOperationRecord::getTenementId, bo.getTenementIds());
        lqw.in(CollectionUtils.isNotEmpty(bo.getStateList()), EWorkOperationRecord::getOperationState, bo.getStateList());
        lqw.in(CollectionUtils.isNotEmpty(bo.getWorkTypes()), EWorkOperationRecord::getWorkType, bo.getWorkType());
        lqw.between(Objects.nonNull(bo.getStartTime()) && Objects.nonNull(bo.getEndTime()),
            EWorkOperationRecord::getOperationTime, bo.getStartTime(), bo.getEndTime());
        return lqw;
    }

    /**
     * 新增工作操作记录
     *
     * @param bo 工作操作记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EWorkOperationRecordBo bo) {
        EWorkOperationRecord add = MapstructUtils.convert(bo, EWorkOperationRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工作操作记录
     *
     * @param bo 工作操作记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EWorkOperationRecordBo bo) {
        EWorkOperationRecord update = MapstructUtils.convert(bo, EWorkOperationRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<EWorkOperationRecord> queryOperationRecords(EWorkOperationRecordBo bo) {
        return baseMapper.selectList(this.buildQueryWrapper(bo));
    }
}
