package com.doumee.service.business.impl; 
 | 
  
 | 
import com.doumee.biz.system.SystemDictDataBiz; 
 | 
import com.doumee.core.constants.ResponseStatus; 
 | 
import com.doumee.core.exception.BusinessException; 
 | 
import com.doumee.core.model.LoginUserInfo; 
 | 
import com.doumee.core.model.PageData; 
 | 
import com.doumee.core.model.PageWrap; 
 | 
import com.doumee.core.utils.Constants; 
 | 
import com.doumee.core.utils.Utils; 
 | 
import com.doumee.dao.business.BrandMapper; 
 | 
import com.doumee.dao.business.model.Brand; 
 | 
import com.doumee.dao.business.model.Category; 
 | 
import com.doumee.dao.business.model.Goods; 
 | 
import com.doumee.service.business.BrandService; 
 | 
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.util.CollectionUtils; 
 | 
  
 | 
import java.util.Date; 
 | 
import java.util.List; 
 | 
import java.util.Objects; 
 | 
  
 | 
/** 
 | 
 * 品牌信息表Service实现 
 | 
 * @author 江蹄蹄 
 | 
 * @date 2023/05/12 13:58 
 | 
 */ 
 | 
@Service 
 | 
public class BrandServiceImpl implements BrandService { 
 | 
  
 | 
    @Autowired 
 | 
    private BrandMapper brandMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private SystemDictDataBiz systemDictDataBiz; 
 | 
    @Override 
 | 
    public Integer create(Brand brand) { 
 | 
        if(StringUtils.isBlank(brand.getName())){ 
 | 
            throw new BusinessException(ResponseStatus.BAD_REQUEST); 
 | 
        } 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        if(brandMapper.selectCount(new QueryWrapper<Brand>() 
 | 
                .eq("ISDELETED",Constants.ZERO) 
 | 
                 .and(user.getType().equals(Constants.UserType.SYSTEM.getKey()), wapper->wapper.lambda().eq(Brand::getType,Constants.ONE).eq(Brand::getName,brand.getName())) 
 | 
//               .eq(user.getType().equals(Constants.UserType.SYSTEM.getKey()),"name",brand.getName()) 
 | 
                .and(user.getType().equals(Constants.UserType.COMPANY.getKey()), 
 | 
                        i->i.apply(" name = '"+brand.getName()+"' and type = 1 ") 
 | 
                                .or().apply( "  name = '"+brand.getName()+"' and COMPANY_ID = '"+user.getCompanyId()+"' and type = 0 ") 
 | 
                ) 
 | 
        )>0){ 
 | 
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+brand.getName()+"】已存在"); 
 | 
        } 
 | 
        if(Constants.equalsInteger(user.getType(), Constants.UserType.SYSTEM.getKey())){ 
 | 
            //平台品牌 
 | 
            brand.setType(Constants.ONE); 
 | 
        }else{ 
 | 
            brand.setType(Constants.ZERO); 
 | 
            brand.setCompanyId(user.getCompanyId()); 
 | 
        } 
 | 
        brand.setStatus(Constants.ZERO); 
 | 
        brand.setCreateDate(new Date()); 
 | 
        brand.setCreator(user.getId()); 
 | 
        brand.setIsdeleted(Constants.ZERO); 
 | 
        brandMapper.insert(brand); 
 | 
        return brand.getId(); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void deleteById(Integer id) { 
 | 
        Brand brand = brandMapper.selectById(id); 
 | 
        if(Objects.isNull(brand)){ 
 | 
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到对象信息"); 
 | 
        } 
 | 
        brand.setIsdeleted(Constants.ONE); 
 | 
        brandMapper.updateById(brand); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void delete(Brand brand) { 
 | 
        UpdateWrapper<Brand> deleteWrapper = new UpdateWrapper<>(brand); 
 | 
        brandMapper.delete(deleteWrapper); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void deleteByIdInBatch(List<Integer> ids) { 
 | 
        if (CollectionUtils.isEmpty(ids)) { 
 | 
            return; 
 | 
        } 
 | 
        brandMapper.deleteBatchIds(ids); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void updateById(Brand brand) { 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
  
 | 
        Brand brand1 = brandMapper.selectOne(new QueryWrapper<Brand>() 
 | 
                .eq("ISDELETED", Constants.ZERO) 
 | 
                  .and(user.getType().equals(Constants.UserType.SYSTEM.getKey()), wapper->wapper.lambda().eq(Brand::getType,Constants.ONE).eq(Brand::getName,brand.getName())) 
 | 
//              .eq(user.getType().equals(Constants.UserType.SYSTEM.getKey()), "name", brand.getName()) 
 | 
                .and(user.getType().equals(Constants.UserType.COMPANY.getKey()), 
 | 
                        i -> i.apply(" name = '" + brand.getName() + "' and type = 1 ") 
 | 
                                .or().apply("  name = '" + brand.getName() + "' and COMPANY_ID = '" + user.getCompanyId() + "' and type = 0 ") 
 | 
                ).last("limit 1") 
 | 
        ); 
 | 
  
 | 
        if(Objects.nonNull(brand1) && (!brand.getId().equals(brand.getId()))){ 
 | 
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+brand.getName()+"】已存在"); 
 | 
        } 
 | 
        brand.setType(null); 
 | 
        brand.setCompanyId(null); 
 | 
        brand.setStatus(Constants.ZERO); 
 | 
        brand.setEditDate(new Date()); 
 | 
        brand.setEditor(user.getId()); 
 | 
        brand.setIsdeleted(Constants.ZERO); 
 | 
        brandMapper.updateById(brand); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void updateByIdInBatch(List<Brand> brands) { 
 | 
        if (CollectionUtils.isEmpty(brands)) { 
 | 
            return; 
 | 
        } 
 | 
        for (Brand brand: brands) { 
 | 
            this.updateById(brand); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Brand findById(Integer id) { 
 | 
        Brand brand = brandMapper.selectById(id); 
 | 
        if(StringUtils.isNotBlank(brand.getImgurl())){ 
 | 
            String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode() 
 | 
                    + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode(); 
 | 
            brand.setImgfullurl(prefixUrl+brand.getImgurl()); 
 | 
        } 
 | 
        return brand; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Brand findOne(Brand brand) { 
 | 
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand); 
 | 
        return brandMapper.selectOne(wrapper); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public List<Brand> findList(Brand brand) { 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand); 
 | 
        wrapper.lambda().eq(Brand::getIsdeleted,Constants.ZERO); 
 | 
//        wrapper.lambda().eq(Brand::getStatus,Constants.ZERO); 
 | 
        wrapper.lambda().orderByAsc(Brand::getType); 
 | 
        wrapper.lambda().orderByAsc(Brand::getSortnum); 
 | 
        // 类型 0企业 1平台 
 | 
        if (Constants.equalsInteger(user.getType(), Constants.UserType.SYSTEM.getKey())) { 
 | 
            wrapper.lambda().eq(Brand::getType,Constants.ONE); 
 | 
        }else { 
 | 
            wrapper.lambda().exists(!Objects.isNull(brand.getCategoryId())," select 1 from goods g where g.brand_id = brand.id and g.category_id = "+brand.getCategoryId()+" and  g.COMPANY_ID = "+user.getCompanyId()+" " + 
 | 
                    " and g.STATUS = 0 and g.ISDELETED = 0  "); 
 | 
            wrapper.lambda().and(i->i.eq(Brand::getCompanyId,user.getCompanyId()) 
 | 
                            .or().eq(Brand::getType,Constants.ONE) 
 | 
            ); 
 | 
        } 
 | 
        List<Brand> list = brandMapper.selectList(wrapper); 
 | 
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode() 
 | 
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode(); 
 | 
        for (Brand b:list) { 
 | 
            if(StringUtils.isNotBlank(b.getImgurl())){ 
 | 
                b.setImgfullurl(prefixUrl+b.getImgurl()); 
 | 
            } 
 | 
        } 
 | 
        return list; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public PageData<Brand> findPage(PageWrap<Brand> pageWrap) { 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        IPage<Brand> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); 
 | 
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>(); 
 | 
        Utils.MP.blankToNull(pageWrap.getModel()); 
 | 
        queryWrapper.lambda().eq(Brand::getIsdeleted, Constants.ZERO); 
 | 
        if (pageWrap.getModel().getName() != null) { 
 | 
            queryWrapper.lambda().like(Brand::getName, pageWrap.getModel().getName()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getStatus() != null) { 
 | 
            queryWrapper.lambda().eq(Brand::getStatus, pageWrap.getModel().getStatus()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getType() != null) { 
 | 
            queryWrapper.lambda().eq(Brand::getType, pageWrap.getModel().getType()); 
 | 
        } 
 | 
        // 类型 0企业 1平台 
 | 
        if (Objects.isNull(user.getCompanyId())) { 
 | 
            queryWrapper.lambda().eq(Brand::getType,Constants.ONE); 
 | 
        }else { 
 | 
            queryWrapper.lambda().and(i->i.eq(Brand::getCompanyId,user.getCompanyId()) 
 | 
                    .or().eq(Brand::getType,Constants.ONE) 
 | 
            ); 
 | 
        } 
 | 
        queryWrapper.lambda().orderByAsc(Brand::getType); 
 | 
        queryWrapper.lambda().orderByAsc(Brand::getSortnum); 
 | 
        IPage<Brand> brandIPage = brandMapper.selectPage(page, queryWrapper); 
 | 
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode() 
 | 
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode(); 
 | 
        brandIPage.getRecords().forEach(i->{ 
 | 
            if(StringUtils.isNotBlank(i.getImgurl())){ 
 | 
                i.setImgfullurl(prefixUrl+i.getImgurl()); 
 | 
            } 
 | 
        }); 
 | 
        return PageData.from(brandIPage); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public long count(Brand brand) { 
 | 
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand); 
 | 
        return brandMapper.selectCount(wrapper); 
 | 
    } 
 | 
} 
 |