package org.dromara.system.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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysAreaBo;
import org.dromara.system.domain.vo.SysAreaVo;
import org.dromara.system.domain.SysArea;
import org.dromara.system.mapper.SysAreaMapper;
import org.dromara.system.service.ISysAreaService;

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

/**
 * 区域Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-18
 */
@RequiredArgsConstructor
@Service
public class SysAreaServiceImpl implements ISysAreaService {

    private final SysAreaMapper baseMapper;

    /**
     * 查询区域
     */
    @Override
    public SysAreaVo queryById(Long areaId){
        return baseMapper.selectVoById(areaId);
    }

    /**
     * 查询区域列表
     */
    @Override
    public TableDataInfo<SysAreaVo> queryPageList(SysAreaBo bo, PageQuery pageQuery) {
        QueryWrapper<SysArea> lqw = buildQueryWrapper(bo);
        Page<SysAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询区域列表
     */
    @Override
    public List<SysAreaVo> queryList(SysAreaBo bo) {
        bo.setIsUse(1);
        QueryWrapper<SysArea> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc("sort");
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<SysArea> buildQueryWrapper(SysAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<SysArea> lqw = new QueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getAreaName()), "area_name", bo.getAreaName());
        lqw.eq(bo.getUp() != null, "up", bo.getUp());
        lqw.eq(bo.getLevel() != null, "level", bo.getLevel());
        lqw.eq(bo.getSort() != null, "sort", bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), "code", bo.getCode());
        lqw.eq(bo.getIsUse() != null, "is_use", bo.getIsUse());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaCode()), "area_code", bo.getAreaCode());
        return lqw;
    }

    /**
     * 新增区域
     */
    @Override
    public Boolean insertByBo(SysAreaBo bo) {
        SysArea add = MapstructUtils.convert(bo, SysArea.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAreaId(add.getAreaId());
        }
        return flag;
    }

    /**
     * 修改区域
     */
    @Override
    public Boolean updateByBo(SysAreaBo bo) {
        SysArea update = MapstructUtils.convert(bo, SysArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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