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.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.domain.checkPoint.CheckPointManage;
import org.dromara.domain.checkPoint.bo.CheckPointManageBo;
import org.dromara.domain.checkPoint.vo.CheckPointManageVo;
import org.dromara.mapper.CheckPointManageMapper;
import org.dromara.service.ICheckPointManageService;
import org.springframework.stereotype.Service;

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

/**
 * 巡更点管理Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class CheckPointManageServiceImpl implements ICheckPointManageService {

    private final CheckPointManageMapper baseMapper;

    /**
     * 查询巡更点管理
     *
     * @param checkPointManageId 主键
     * @return 巡更点管理
     */
    @Override
    public CheckPointManageVo queryById(Long checkPointManageId){
        return baseMapper.selectVoById(checkPointManageId);
    }

    /**
     * 分页查询巡更点管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 巡更点管理分页列表
     */
    @Override
    public TableDataInfo<CheckPointManageVo> queryPageList(CheckPointManageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CheckPointManage> lqw = buildQueryWrapper(bo);
        Page<CheckPointManageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的巡更点管理列表
     *
     * @param bo 查询条件
     * @return 巡更点管理列表
     */
    @Override
    public List<CheckPointManageVo> queryList(CheckPointManageBo bo) {
        LambdaQueryWrapper<CheckPointManage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CheckPointManage> buildQueryWrapper(CheckPointManageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CheckPointManage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CheckPointManage::getCheckPointManageId);
        lqw.eq(StringUtils.isNotBlank(bo.getDateTimeNum()), CheckPointManage::getDateTimeNum, bo.getDateTimeNum());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckType()), CheckPointManage::getCheckType, bo.getCheckType());
        lqw.eq(bo.getCheckTime() != null, CheckPointManage::getCheckTime, bo.getCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckAddress()), CheckPointManage::getCheckAddress, bo.getCheckAddress());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), CheckPointManage::getUserName, bo.getUserName());
        lqw.eq(bo.getUseId() != null, CheckPointManage::getUseId, bo.getUseId());
        return lqw;
    }

    /**
     * 新增巡更点管理
     *
     * @param bo 巡更点管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CheckPointManageBo bo) {
        CheckPointManage add = MapstructUtils.convert(bo, CheckPointManage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCheckPointManageId(add.getCheckPointManageId());
        }
        return flag;
    }

    /**
     * 修改巡更点管理
     *
     * @param bo 巡更点管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CheckPointManageBo bo) {
        CheckPointManage update = MapstructUtils.convert(bo, CheckPointManage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean batchCheckPointManage(List<CheckPointManage> checkPointManageList) {
        for (CheckPointManage pointManage : checkPointManageList) {
            pointManage.setCreateTime(DateUtils.getNowDate());
        }
        boolean flag = baseMapper.insertBatch(checkPointManageList);
        return flag;
    }
}
