package com.panzhihua.service_community.service.impl; import static java.util.Objects.isNull; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.panzhihua.common.model.dtos.community.convenient.ConvenientServiceCategoryDTO; import com.panzhihua.common.model.dtos.community.convenient.PageConvenientServiceCategoryDTO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.convenient.ConvenientServiceCategoryVO; import com.panzhihua.service_community.dao.ConvenientServiceCategoryDAO; import com.panzhihua.service_community.model.dos.ConvenientServiceCategoryDO; import com.panzhihua.service_community.service.ConvenientServiceCategoryService; /** * @title: ConvenientServiceCategoryServiceImpl * @projectName: 成都呐喊信息技术有限公司-智慧社区项目 * @description: 便民服务分类服务实现类 * @author: hans * @date: 2021/09/16 10:56 */ @Service public class ConvenientServiceCategoryServiceImpl extends ServiceImpl implements ConvenientServiceCategoryService { @Override public R addServiceCategory(ConvenientServiceCategoryDTO convenientServiceCategoryDTO) { ConvenientServiceCategoryDO convenientServiceCategoryDO = new ConvenientServiceCategoryDO(); BeanUtils.copyProperties(convenientServiceCategoryDTO, convenientServiceCategoryDO); convenientServiceCategoryDO.setCreatedAt(new Date()); int result = this.baseMapper.insert(convenientServiceCategoryDO); if (result > 0) { return R.ok(); } return R.fail("添加失败"); } @Override public R putServiceCategory(ConvenientServiceCategoryDTO convenientServiceCategoryDTO) { ConvenientServiceCategoryDO convenientServiceCategoryDO = this.baseMapper.selectById(convenientServiceCategoryDTO.getId()); if (isNull(convenientServiceCategoryDO)) { return R.fail("分类id不存在"); } BeanUtils.copyProperties(convenientServiceCategoryDTO, convenientServiceCategoryDO); int result = this.baseMapper.updateById(convenientServiceCategoryDO); if (result > 0) { return R.ok(); } return R.fail("更新失败"); } @Override public R deleteServiceCategoryById(Long categoryId, Long operator) { if (isNull(categoryId)) { return R.fail("分类id不能为空"); } ConvenientServiceCategoryDO convenientServiceCategoryDO = this.baseMapper.selectById(categoryId); if (isNull(convenientServiceCategoryDO)) { return R.fail("分类id不存在"); } int count = this.baseMapper.checkCategoryIsUsing(categoryId); if (count > 0) { return R.fail("该分类名称已被引用,无法删除!"); } convenientServiceCategoryDO.setIsDel(true); convenientServiceCategoryDO.setUpdatedBy(operator); int result = this.baseMapper.updateById(convenientServiceCategoryDO); if (result > 0) { return R.ok(); } return R.fail("删除失败"); } @Override public R getServiceCategoryById(Long categoryId) { if (isNull(categoryId)) { return R.fail("分类id不能为空"); } ConvenientServiceCategoryDO convenientServiceCategoryDO = this.baseMapper.selectById(categoryId); if (isNull(convenientServiceCategoryDO)) { return R.fail("分类id不存在"); } ConvenientServiceCategoryVO convenientServiceCategoryVO = new ConvenientServiceCategoryVO(); BeanUtils.copyProperties(convenientServiceCategoryDO, convenientServiceCategoryVO); return R.ok(convenientServiceCategoryVO); } @Override public R pageServiceCategory(PageConvenientServiceCategoryDTO pageConvenientServiceCategoryDTO) { Page page = new Page<>(); page.setSize(pageConvenientServiceCategoryDTO.getPageSize()); page.setCurrent(pageConvenientServiceCategoryDTO.getPageNum()); IPage iPage = this.baseMapper.pageServiceCategory(page, pageConvenientServiceCategoryDTO); return R.ok(iPage); } @Override public R getAllServiceCategories(String areaCode) { List categoryVOList = new ArrayList<>(); List categoryDOS = this.baseMapper.selectList(new QueryWrapper() .lambda().eq(ConvenientServiceCategoryDO::getAreaCode,areaCode).orderByDesc(ConvenientServiceCategoryDO::getWeight)); if (!ObjectUtils.isEmpty(categoryDOS)) { categoryDOS.forEach(categoryDO -> { ConvenientServiceCategoryVO categoryVO = new ConvenientServiceCategoryVO(); BeanUtils.copyProperties(categoryDO, categoryVO); categoryVOList.add(categoryVO); }); } return R.ok(categoryVOList); } @Override public R getSuitableServiceCategories(Long communityId,String areaCode) { return R.ok(this.baseMapper.selectSuitableServiceCategories(communityId,areaCode)); } }