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.HomeServicePackagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServicePackageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServicePackageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServicePackageDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServicePackageMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

@Service
@Validated
public class HomeServicePackageServiceImpl implements HomeServicePackageService {

    @Resource
    private HomeServicePackageMapper homeServicePackageMapper;

    @Override
    public Long createHomeServicePackage(HomeServicePackageSaveReqVO createReqVO) {
        HomeServicePackageDO packageDO = BeanUtils.toBean(createReqVO, HomeServicePackageDO.class);
        homeServicePackageMapper.insert(packageDO);
        return packageDO.getId();
    }

    @Override
    public void updateHomeServicePackage(HomeServicePackageSaveReqVO updateReqVO) {
        HomeServicePackageDO packageDO = BeanUtils.toBean(updateReqVO, HomeServicePackageDO.class);
        homeServicePackageMapper.updateById(packageDO);
    }

    @Override
    public void deleteHomeServicePackage(Long id) {
        homeServicePackageMapper.deleteById(id);
    }

    @Override
    public HomeServicePackageRespVO getHomeServicePackage(Long id) {
        return BeanUtils.toBean(homeServicePackageMapper.selectById(id), HomeServicePackageRespVO.class);
    }

    @Override
    public PageResult<HomeServicePackageRespVO> getHomeServicePackagePage(HomeServicePackagePageReqVO pageReqVO) {
        LambdaQueryWrapperX<HomeServicePackageDO> query = new LambdaQueryWrapperX<HomeServicePackageDO>()
                .eqIfPresent(HomeServicePackageDO::getCategoryId, pageReqVO.getCategoryId())
                .likeIfPresent(HomeServicePackageDO::getPackageName, pageReqVO.getPackageName())
                .eqIfPresent(HomeServicePackageDO::getStatus, pageReqVO.getStatus())
                .eqIfPresent(HomeServicePackageDO::getTenantId, pageReqVO.getTenantId())
                .inIfPresent(HomeServicePackageDO::getTenantId, pageReqVO.getTenantIds())
                .orderByDesc(HomeServicePackageDO::getId);

        PageResult<HomeServicePackageDO> pageResult = homeServicePackageMapper.selectPage(pageReqVO, query);
        return BeanUtils.toBean(pageResult, HomeServicePackageRespVO.class);
    }
}



