package org.dromara.system.service.impl;

import org.dromara.common.core.domain.model.LoginUser;
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.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.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.OrientationInfoBo;
import org.dromara.system.domain.vo.OrientationInfoVo;
import org.dromara.system.domain.OrientationInfo;
import org.dromara.system.mapper.OrientationInfoMapper;
import org.dromara.system.service.IOrientationInfoService;

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

/**
 * 定位信息Service业务层处理
 *
 * @author boman
 * @date 2023-08-30
 */
@RequiredArgsConstructor
@Service
public class OrientationInfoServiceImpl implements IOrientationInfoService {

    private final OrientationInfoMapper baseMapper;

    /**
     * 查询定位信息
     */
    @Override
    public OrientationInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定位信息列表
     */
    @Override
    public TableDataInfo<OrientationInfoVo> queryPageList(OrientationInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrientationInfo> lqw = buildQueryWrapper(bo);
        Page<OrientationInfoVo> result = baseMapper.selectVoMapperPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定位信息列表
     */
    @Override
    public List<OrientationInfoVo> queryList(OrientationInfoBo bo) {
        LambdaQueryWrapper<OrientationInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrientationInfo> buildQueryWrapper(OrientationInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrientationInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, OrientationInfo::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), OrientationInfo::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), OrientationInfo::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), OrientationInfo::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), OrientationInfo::getLatitude, bo.getLatitude());
        return lqw;
    }

    /**
     * 新增定位信息
     */
    @Override
    public Boolean insertByBo(OrientationInfoBo bo) {
        LoginUser user = LoginHelper.getLoginUser();
        bo.setUserId(user.getUserId());
        bo.setUserName(user.getUsername());
        bo.setCreateTime(DateUtils.getNowDate());
        bo.setUpdateTime(DateUtils.getNowDate());
        OrientationInfo add = MapstructUtils.convert(bo, OrientationInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定位信息
     */
    @Override
    public Boolean updateByBo(OrientationInfoBo bo) {
        OrientationInfo update = MapstructUtils.convert(bo, OrientationInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除定位信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
