package com.sz.admin.monitor.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.monitor.mapper.RegionMapper;
import com.sz.admin.monitor.pojo.dto.region.RegionCreateDTO;
import com.sz.admin.monitor.pojo.dto.region.RegionListDTO;
import com.sz.admin.monitor.pojo.dto.region.RegionUpdateDTO;
import com.sz.admin.monitor.pojo.po.Region;
import com.sz.admin.monitor.pojo.vo.Region.RegionVO;
import com.sz.admin.monitor.service.RegionService;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.datascope.SimpleDataScopeHelper;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.PageUtils;
import com.sz.core.util.Utils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: RegionServiceImpl
 * Package: com.sz.admin.monitor.service.impl
 * Description:
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    @Override
    public void create(RegionCreateDTO dto) {
        Region region = BeanCopyUtils.copy(dto, Region.class);
        // 唯一性校验
        save(region);
    }

    @Override
    public void update(RegionUpdateDTO dto) {
        Region region = BeanCopyUtils.copy(dto, Region.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create().eq(Region::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);
        // 唯一性校验
        saveOrUpdate(region);
    }

    @Override
    public PageResult<RegionVO> page(RegionListDTO dto) {
        try {
            SimpleDataScopeHelper.start(Region.class); // 指定要追加条件的表PO实体
            Page<RegionVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), RegionVO.class); // 调试sql
            return PageUtils.getPageResult(page);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public List<RegionVO> list(RegionListDTO dto) {
        try {
            SimpleDataScopeHelper.start(Region.class); // 指定要追加条件的表PO实体
            return listAs(buildQueryWrapper(dto), RegionVO.class);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public RegionVO detail(Long id) {
        Region region = getById(id);
        CommonResponseEnum.INVALID_ID.assertNull(region);
        return BeanCopyUtils.copy(region, RegionVO.class);
    }



  

    private static QueryWrapper buildQueryWrapper(RegionListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(Region.class);
        if (Utils.isNotNull(dto.getCode())) {
            wrapper.eq(Region::getCode, dto.getCode());
        }
        if (Utils.isNotNull(dto.getName())) {
            wrapper.like(Region::getName, dto.getName());
        }

        if (Utils.isNotNull(dto.getLevel())) {
            wrapper.eq(Region::getLevel, dto.getLevel());
        }
        if (Utils.isNotNull(dto.getType())) {
            wrapper.eq(Region::getType, dto.getType());
        }
        return wrapper;
    }

  
}
