package doumeemes.service.business.impl;
|
|
import doumeemes.core.constants.ResponseStatus;
|
import doumeemes.core.exception.BusinessException;
|
import doumeemes.core.model.ApiResponse;
|
import doumeemes.core.model.LoginUserInfo;
|
import doumeemes.core.model.PageData;
|
import doumeemes.core.model.PageWrap;
|
import doumeemes.core.utils.Constants;
|
import doumeemes.core.utils.Utils;
|
import doumeemes.dao.business.UnitMapper;
|
import doumeemes.dao.business.model.Unit;
|
import doumeemes.service.business.UnitService;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.shiro.SecurityUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 物料单位信息表Service实现
|
* @author 江蹄蹄
|
* @date 2022/04/20 09:37
|
*/
|
@Service
|
public class UnitServiceImpl implements UnitService {
|
|
@Autowired
|
private UnitMapper unitMapper;
|
|
@Override
|
@Transactional
|
public Integer create(Unit unit) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
unit.setDeleted(Constants.ZERO);
|
unit.setCreateTime(new Date());
|
unit.setCreateUser(user.getId());
|
unit.setRootDepartId(user.getRootDepartment().getId());
|
unit.setStatus(Constants.ONE);
|
unit.setType(Constants.ZERO);
|
|
Unit find=new Unit();
|
find.setRootDepartId(user.getRootDepartment().getId());
|
find.setDeleted(Constants.ZERO);
|
find.setName(unit.getName());
|
if( this.count(find)>0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"单位名称不能重复");
|
}else{
|
unitMapper.insert(unit);
|
}
|
|
return 1;
|
}
|
public Integer createOld(Unit unit) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
if(StringUtils.isNotEmpty(unit.getTypes())){
|
String [] str=unit.getTypes().split(",");
|
for(String tp:str){
|
unit.setDeleted(Constants.ZERO);
|
unit.setCreateTime(new Date());
|
unit.setCreateUser(user.getId());
|
unit.setRootDepartId(user.getRootDepartment().getId());
|
unit.setStatus(Constants.ONE);
|
unit.setType(Integer.valueOf(tp));
|
|
Unit find=new Unit();
|
find.setRootDepartId(user.getRootDepartment().getId());
|
find.setDeleted(Constants.ZERO);
|
find.setType(Integer.valueOf(tp));
|
find.setName(unit.getName());
|
List<Unit> findList= this.findList(find);
|
|
if(findList.size()>0){
|
throw new RuntimeException("同一单位类型下名称不能重复");
|
}else{
|
unitMapper.insert(unit);
|
}
|
}
|
}
|
|
return 1;
|
}
|
|
@Override
|
public void deleteById(Integer id) {
|
Unit unit= this.findById(id);
|
unit.setDeleted(Constants.ONE);
|
unitMapper.updateById(unit);
|
}
|
|
@Override
|
public void delete(Unit unit) {
|
UpdateWrapper<Unit> deleteWrapper = new UpdateWrapper<>(unit);
|
unitMapper.delete(deleteWrapper);
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
for(Integer id:ids){
|
Unit unit= this.findById(id);
|
unit.setDeleted(Constants.ONE);
|
unitMapper.updateById(unit);
|
// unitMapper.deleteBatchIds(ids);
|
}
|
|
}
|
|
@Override
|
public ApiResponse updateById(Unit unit) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
unit.setUpdateTime(new Date());
|
unit.setUpdateUser(user.getId());
|
|
Unit find=new Unit();
|
find.setRootDepartId(user.getRootDepartment().getId());
|
find.setDeleted(Constants.ZERO);
|
find.setType(unit.getType());
|
find.setName(unit.getName());
|
find.setAttributeData(unit.getAttributeData());
|
List<Unit> findList= this.findList(find);
|
if(findList.size()>0){
|
if(findList.get(0).getId()!=unit.getId()){
|
return ApiResponse.failed("同一单位类型下名称不能重复");
|
}
|
}
|
unitMapper.updateById(unit);
|
return ApiResponse.success(null);
|
|
}
|
|
@Override
|
public void updateByIdInBatch(List<Unit> units) {
|
if (CollectionUtils.isEmpty(units)) {
|
return;
|
}
|
for (Unit unit: units) {
|
this.updateById(unit);
|
}
|
}
|
|
@Override
|
public Unit findById(Integer id) {
|
return unitMapper.selectById(id);
|
}
|
|
@Override
|
public Unit findOne(Unit unit) {
|
QueryWrapper<Unit> wrapper = new QueryWrapper<>(unit);
|
return unitMapper.selectOne(wrapper);
|
}
|
|
@Override
|
public List<Unit> findList(Unit unit) {
|
QueryWrapper<Unit> wrapper = new QueryWrapper<>(unit);
|
return unitMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<Unit> findPage(PageWrap<Unit> pageWrap) {
|
IPage<Unit> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
QueryWrapper<Unit> queryWrapper = new QueryWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
if (pageWrap.getModel().getId() != null) {
|
queryWrapper.lambda().eq(Unit::getId, pageWrap.getModel().getId());
|
}
|
if (pageWrap.getModel().getDeleted() != null) {
|
queryWrapper.lambda().eq(Unit::getDeleted, pageWrap.getModel().getDeleted());
|
}
|
if (pageWrap.getModel().getCreateUser() != null) {
|
queryWrapper.lambda().eq(Unit::getCreateUser, pageWrap.getModel().getCreateUser());
|
}
|
if (pageWrap.getModel().getCreateTime() != null) {
|
queryWrapper.lambda().ge(Unit::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime()));
|
queryWrapper.lambda().le(Unit::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime()));
|
}
|
if (pageWrap.getModel().getUpdateUser() != null) {
|
queryWrapper.lambda().eq(Unit::getUpdateUser, pageWrap.getModel().getUpdateUser());
|
}
|
if (pageWrap.getModel().getUpdateTime() != null) {
|
queryWrapper.lambda().ge(Unit::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime()));
|
queryWrapper.lambda().le(Unit::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime()));
|
}
|
if (pageWrap.getModel().getRemark() != null) {
|
queryWrapper.lambda().eq(Unit::getRemark, pageWrap.getModel().getRemark());
|
}
|
if (pageWrap.getModel().getRootDepartId() != null) {
|
queryWrapper.lambda().eq(Unit::getRootDepartId, pageWrap.getModel().getRootDepartId());
|
}
|
if (pageWrap.getModel().getName() != null) {
|
queryWrapper.lambda().eq(Unit::getName, pageWrap.getModel().getName());
|
}
|
if (pageWrap.getModel().getType() != null) {
|
queryWrapper.lambda().eq(Unit::getType, pageWrap.getModel().getType());
|
}
|
if (pageWrap.getModel().getStatus() != null) {
|
queryWrapper.lambda().eq(Unit::getStatus, pageWrap.getModel().getStatus());
|
}
|
for(PageWrap.SortData sortData: pageWrap.getSorts()) {
|
if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
|
queryWrapper.orderByDesc(sortData.getProperty());
|
} else {
|
queryWrapper.orderByAsc(sortData.getProperty());
|
}
|
}
|
return PageData.from(unitMapper.selectPage(page, queryWrapper));
|
}
|
|
@Override
|
public long count(Unit unit) {
|
QueryWrapper<Unit> wrapper = new QueryWrapper<>(unit);
|
return unitMapper.selectCount(wrapper);
|
}
|
}
|