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.HomeServiceDepartmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceDepartmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceDepartmentSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceDepartmentDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceDepartmentMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

@Service
@Validated
public class HomeServiceDepartmentServiceImpl implements HomeServiceDepartmentService {

    @Resource
    private HomeServiceDepartmentMapper homeServiceDepartmentMapper;

    @Override
    public Long createHomeServiceDepartment(HomeServiceDepartmentSaveReqVO createReqVO) {
        HomeServiceDepartmentDO deptDO = BeanUtils.toBean(createReqVO, HomeServiceDepartmentDO.class);
        homeServiceDepartmentMapper.insert(deptDO);
        return deptDO.getId();
    }

    @Override
    public void updateHomeServiceDepartment(HomeServiceDepartmentSaveReqVO updateReqVO) {
        HomeServiceDepartmentDO deptDO = BeanUtils.toBean(updateReqVO, HomeServiceDepartmentDO.class);
        homeServiceDepartmentMapper.updateById(deptDO);
    }

    @Override
    public void deleteHomeServiceDepartment(Long id) {
        homeServiceDepartmentMapper.deleteById(id);
    }

    @Override
    public HomeServiceDepartmentRespVO getHomeServiceDepartment(Long id) {
        return BeanUtils.toBean(homeServiceDepartmentMapper.selectById(id), HomeServiceDepartmentRespVO.class);
    }

    @Override
    public PageResult<HomeServiceDepartmentRespVO> getHomeServiceDepartmentPage(HomeServiceDepartmentPageReqVO pageReqVO) {
        LambdaQueryWrapperX<HomeServiceDepartmentDO> query = new LambdaQueryWrapperX<HomeServiceDepartmentDO>()
                .eqIfPresent(HomeServiceDepartmentDO::getSuperiorId, pageReqVO.getSuperiorId())
                .likeIfPresent(HomeServiceDepartmentDO::getName, pageReqVO.getName())
                .eqIfPresent(HomeServiceDepartmentDO::getResponsiblePersonId, pageReqVO.getResponsiblePersonId())
                .eqIfPresent(HomeServiceDepartmentDO::getTenantId, pageReqVO.getTenantId())
                .inIfPresent(HomeServiceDepartmentDO::getTenantId, pageReqVO.getTenantIds())
                .orderByDesc(HomeServiceDepartmentDO::getId);

        PageResult<HomeServiceDepartmentDO> pageResult = homeServiceDepartmentMapper.selectPage(pageReqVO, query);
        return BeanUtils.toBean(pageResult, HomeServiceDepartmentRespVO.class);
    }
}
