package cn.iocoder.yudao.module.system.service.home;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceCategoryPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceCategoryPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceCategoryRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceCategorySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceCategoryDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

@Service
@Validated
public class HomeServiceCategoryServiceImpl implements HomeServiceCategoryService {

    @Resource
    private HomeServiceCategoryMapper homeServiceCategoryMapper;

    @Override
    public Long createHomeServiceCategory(HomeServiceCategorySaveReqVO createReqVO) {
        HomeServiceCategoryDO categoryDO = BeanUtils.toBean(createReqVO, HomeServiceCategoryDO.class);
        homeServiceCategoryMapper.insert(categoryDO);
        return categoryDO.getId();
    }

    @Override
    public void updateHomeServiceCategory(HomeServiceCategorySaveReqVO updateReqVO) {
        HomeServiceCategoryDO categoryDO = BeanUtils.toBean(updateReqVO, HomeServiceCategoryDO.class);
        homeServiceCategoryMapper.updateById(categoryDO);
    }

    @Override
    public void deleteHomeServiceCategory(Long id) {
        homeServiceCategoryMapper.deleteById(id);
    }

    @Override
    public HomeServiceCategoryRespVO getHomeServiceCategory(Long id) {
        return BeanUtils.toBean(homeServiceCategoryMapper.selectById(id), HomeServiceCategoryRespVO.class);
    }

    @Override
    public PageResult<HomeServiceCategoryPageRespVO> getHomeServiceCategoryPage(HomeServiceCategoryPageReqVO pageReqVO) {
        LambdaQueryWrapperX<HomeServiceCategoryDO> query = new LambdaQueryWrapperX<HomeServiceCategoryDO>()
                .eqIfPresent(HomeServiceCategoryDO::getSuperiorId, pageReqVO.getSuperiorId())
                .likeIfPresent(HomeServiceCategoryDO::getName, pageReqVO.getName())
                .eqIfPresent(HomeServiceCategoryDO::getTenantId, pageReqVO.getTenantId())
                .inIfPresent(HomeServiceCategoryDO::getTenantId, pageReqVO.getTenantIds())
                .orderByDesc(HomeServiceCategoryDO::getId);

        PageResult<HomeServiceCategoryDO> pageResult = homeServiceCategoryMapper.selectPage(pageReqVO, query);
        return BeanUtils.toBean(pageResult, HomeServiceCategoryPageRespVO.class);
    }
}
