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.HomeServiceAreaPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceAreaRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceAreaSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceAreaDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceAreaMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

@Service
@Validated
public class HomeServiceAreaServiceImpl implements HomeServiceAreaService {

    @Resource
    private HomeServiceAreaMapper homeServiceAreaMapper;

    @Override
    public Long createHomeServiceArea(HomeServiceAreaSaveReqVO createReqVO) {
        HomeServiceAreaDO areaDO = BeanUtils.toBean(createReqVO, HomeServiceAreaDO.class);
        homeServiceAreaMapper.insert(areaDO);
        return areaDO.getId();
    }

    @Override
    public void updateHomeServiceArea(HomeServiceAreaSaveReqVO updateReqVO) {
        HomeServiceAreaDO areaDO = BeanUtils.toBean(updateReqVO, HomeServiceAreaDO.class);
        homeServiceAreaMapper.updateById(areaDO);
    }

    @Override
    public void deleteHomeServiceArea(Long id) {
        homeServiceAreaMapper.deleteById(id);
    }

    @Override
    public HomeServiceAreaRespVO getHomeServiceArea(Long id) {
        return BeanUtils.toBean(homeServiceAreaMapper.selectById(id), HomeServiceAreaRespVO.class);
    }

    @Override
    public PageResult<HomeServiceAreaRespVO> getHomeServiceAreaPage(HomeServiceAreaPageReqVO pageReqVO) {
        LambdaQueryWrapperX<HomeServiceAreaDO> query = new LambdaQueryWrapperX<HomeServiceAreaDO>()
                .likeIfPresent(HomeServiceAreaDO::getArea, pageReqVO.getArea())
                .likeIfPresent(HomeServiceAreaDO::getRemark, pageReqVO.getRemark())
                .eqIfPresent(HomeServiceAreaDO::getTenantId, pageReqVO.getTenantId())
                .inIfPresent(HomeServiceAreaDO::getTenantId, pageReqVO.getTenantIds())
                .orderByDesc(HomeServiceAreaDO::getId);

        PageResult<HomeServiceAreaDO> pageResult = homeServiceAreaMapper.selectPage(pageReqVO, query);
        return BeanUtils.toBean(pageResult, HomeServiceAreaRespVO.class);
    }
}
