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.HomeServiceItemPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceItemPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceItemRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceItemSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

@Service
@Validated
public class HomeServiceItemServiceImpl implements HomeServiceItemService {

    @Resource
    private HomeServiceItemMapper homeServiceItemMapper;

    @Override
    public Long createHomeServiceItem(HomeServiceItemSaveReqVO createReqVO) {
        HomeServiceItemDO itemDO = BeanUtils.toBean(createReqVO, HomeServiceItemDO.class);
        homeServiceItemMapper.insert(itemDO);
        return itemDO.getId();
    }

    @Override
    public void updateHomeServiceItem(HomeServiceItemSaveReqVO updateReqVO) {
        HomeServiceItemDO itemDO = BeanUtils.toBean(updateReqVO, HomeServiceItemDO.class);
        homeServiceItemMapper.updateById(itemDO);
    }

    @Override
    public void deleteHomeServiceItem(Long id) {
        homeServiceItemMapper.deleteById(id);
    }

    @Override
    public HomeServiceItemRespVO getHomeServiceItem(Long id) {
        return BeanUtils.toBean(homeServiceItemMapper.selectById(id), HomeServiceItemRespVO.class);
    }

    @Override
    public PageResult<HomeServiceItemPageRespVO> getHomeServiceItemPage(HomeServiceItemPageReqVO pageReqVO) {
        LambdaQueryWrapperX<HomeServiceItemDO> query = new LambdaQueryWrapperX<HomeServiceItemDO>()
                .eqIfPresent(HomeServiceItemDO::getCategoryId, pageReqVO.getCategoryId())
                .likeIfPresent(HomeServiceItemDO::getItemName, pageReqVO.getItemName())
                .eqIfPresent(HomeServiceItemDO::getStatus, pageReqVO.getStatus())
                .eqIfPresent(HomeServiceItemDO::getTenantId, pageReqVO.getTenantId())
                .inIfPresent(HomeServiceItemDO::getTenantId, pageReqVO.getTenantIds())
                .orderByDesc(HomeServiceItemDO::getId);

        PageResult<HomeServiceItemDO> pageResult = homeServiceItemMapper.selectPage(pageReqVO, query);
        return BeanUtils.toBean(pageResult, HomeServiceItemPageRespVO.class);
    }
}
