package com.xzx.gc.system.service;  
 | 
  
 | 
import cn.hutool.core.bean.BeanUtil;  
 | 
import cn.hutool.core.collection.CollUtil;  
 | 
import cn.hutool.core.convert.Convert;  
 | 
import cn.hutool.core.date.DateUtil;  
 | 
import com.github.pagehelper.PageHelper;  
 | 
import com.github.pagehelper.PageInfo;  
 | 
import com.xzx.gc.entity.HomeServiceInfo;  
 | 
import com.xzx.gc.entity.HomeServiceRef;  
 | 
import com.xzx.gc.model.order.PartnerFenceDTO;  
 | 
import com.xzx.gc.model.system.HomeServiceAddDTO;  
 | 
import com.xzx.gc.model.system.HomeServicePageDTO;  
 | 
import com.xzx.gc.system.mapper.HomeServiceInfoMapper;  
 | 
import org.springframework.beans.factory.annotation.Autowired;  
 | 
import org.springframework.stereotype.Service;  
 | 
import org.springframework.transaction.annotation.Transactional;  
 | 
import tk.mybatis.mapper.entity.Example;  
 | 
  
 | 
import java.util.ArrayList;  
 | 
import java.util.List;  
 | 
import java.util.stream.Collectors;  
 | 
  
 | 
@Service  
 | 
@Transactional  
 | 
public class HomeServiceInfoService {  
 | 
  
 | 
    @Autowired  
 | 
    private HomeServiceInfoMapper homeServiceInfoMapper;  
 | 
  
 | 
    @Autowired  
 | 
    private HomeServiceRefService homeServiceRefService;  
 | 
  
 | 
    /**  
 | 
     * 添加  
 | 
     */  
 | 
    public Long add(HomeServiceAddDTO homeServiceAddDTO){  
 | 
  
 | 
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();  
 | 
        BeanUtil.copyProperties(homeServiceAddDTO,homeServiceInfo);  
 | 
        homeServiceInfo.setDelFlag(false);  
 | 
        homeServiceInfo.setCreateTime(DateUtil.now());  
 | 
        homeServiceInfo.setId(null);  
 | 
        homeServiceInfoMapper.insertSelective(homeServiceInfo);  
 | 
  
 | 
        //添加二级分类  
 | 
        if(homeServiceAddDTO.getLevel().equals(Convert.toShort(2))){  
 | 
            addRef(homeServiceAddDTO, homeServiceInfo.getId());  
 | 
  
 | 
        }  
 | 
        return homeServiceInfo.getId();  
 | 
    }  
 | 
  
 | 
    /**  
 | 
     * 添加分类与合伙人或围栏的对应关系  
 | 
     * @param homeServiceAddDTO  
 | 
     * @param serviceId  
 | 
     */  
 | 
    private void addRef(HomeServiceAddDTO homeServiceAddDTO, Long serviceId) {  
 | 
        List<PartnerFenceDTO> list = homeServiceAddDTO.getList();  
 | 
  
 | 
        List<HomeServiceRef> listObj=CollUtil.newArrayList();  
 | 
  
 | 
        for (PartnerFenceDTO partnerFenceDTO : list) {  
 | 
            List<String> fenceId = partnerFenceDTO.getFenceIds();  
 | 
            String partnerId = partnerFenceDTO.getPartnerId();  
 | 
            if(CollUtil.isEmpty(fenceId)){  
 | 
                HomeServiceRef homeServiceRef=new HomeServiceRef();  
 | 
                homeServiceRef.setServiceId(serviceId);  
 | 
                homeServiceRef.setRefType(Convert.toShort(1));  
 | 
                homeServiceRef.setPartnerId(partnerId);  
 | 
                listObj.add(homeServiceRef);  
 | 
            }else {  
 | 
                for (String s : fenceId) {  
 | 
                    HomeServiceRef homeServiceRef=new HomeServiceRef();  
 | 
                    homeServiceRef.setServiceId(serviceId);  
 | 
                    homeServiceRef.setRefType(Convert.toShort(2));  
 | 
                    homeServiceRef.setPartnerId(partnerId);  
 | 
                    homeServiceRef.setFenceId(s);  
 | 
                    listObj.add(homeServiceRef);  
 | 
                }  
 | 
  
 | 
            }  
 | 
        }  
 | 
  
 | 
        if(CollUtil.isNotEmpty(listObj)){  
 | 
            homeServiceRefService.addBatch(listObj);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    /**  
 | 
     * 根据父ID查询  
 | 
     * @param parentId  
 | 
     * @return  
 | 
     */  
 | 
    public List<HomeServiceInfo> findByParentId(Long parentId,Short showFlag){  
 | 
        Example example=new Example(HomeServiceInfo.class);  
 | 
        Example.Criteria criteria = example.createCriteria();  
 | 
        criteria.andEqualTo("parentId",Convert.toLong(parentId));  
 | 
        criteria.andEqualTo("delFlag",false);  
 | 
        if(showFlag!=null){  
 | 
            criteria.andEqualTo("showFlag",showFlag);  
 | 
        }  
 | 
        example.setOrderByClause("sort asc");  
 | 
        return homeServiceInfoMapper.selectByExample(example);  
 | 
    }  
 | 
  
 | 
  
 | 
    public List<HomeServiceInfo> findByLevel(Short level){  
 | 
        Example example=new Example(HomeServiceInfo.class);  
 | 
        Example.Criteria criteria = example.createCriteria();  
 | 
        criteria.andEqualTo("level",level);  
 | 
        criteria.andEqualTo("delFlag",false);  
 | 
        example.setOrderByClause("create_time desc");  
 | 
        return homeServiceInfoMapper.selectByExample(example);  
 | 
    }  
 | 
  
 | 
  
 | 
    /**  
 | 
     * 分页查询  
 | 
     * @param homeServicePageDTO  
 | 
     * @return  
 | 
     */  
 | 
    public PageInfo<HomeServiceInfo> page(HomeServicePageDTO homeServicePageDTO) {  
 | 
        PageHelper.startPage(homeServicePageDTO.getPageNo(),homeServicePageDTO.getPageSize());  
 | 
        List<HomeServiceInfo> list=homeServiceInfoMapper.page(homeServicePageDTO);  
 | 
        return new PageInfo<>(list);  
 | 
    }  
 | 
  
 | 
    public HomeServiceInfo findById(Long id){  
 | 
        return homeServiceInfoMapper.selectByPrimaryKey(id);  
 | 
    }  
 | 
  
 | 
    public void deleteById(Long id){  
 | 
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();  
 | 
        homeServiceInfo.setId(id);  
 | 
        homeServiceInfo.setDelFlag(true);  
 | 
        homeServiceInfoMapper.updateByPrimaryKeySelective(homeServiceInfo);  
 | 
    }  
 | 
  
 | 
    public void updateById(HomeServiceInfo homeServiceInfo){  
 | 
        homeServiceInfoMapper.updateByPrimaryKeySelective(homeServiceInfo);  
 | 
    }  
 | 
  
 | 
  
 | 
    public void update(HomeServiceAddDTO homeServiceAddDTO) {  
 | 
        //先删除 再加  
 | 
        deleteById(homeServiceAddDTO.getId());  
 | 
        //删除关联关系  
 | 
        if(homeServiceAddDTO.getLevel().intValue()==2){  
 | 
            homeServiceRefService.deleteByServiceId(homeServiceAddDTO.getId());  
 | 
        }  
 | 
        Long add = add(homeServiceAddDTO);  
 | 
  
 | 
        //更改关联关系  
 | 
        if(homeServiceAddDTO.getLevel().intValue()==2) {  
 | 
            updateParentByParentId(homeServiceAddDTO.getId(),add);  
 | 
        }  
 | 
  
 | 
//        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();  
 | 
//        BeanUtil.copyProperties(homeServiceAddDTO,homeServiceInfo);  
 | 
//        updateById(homeServiceInfo);  
 | 
//  
 | 
//        List<PartnerFenceDTO> list = homeServiceAddDTO.getList();  
 | 
//        if(CollUtil.isNotEmpty(list)){  
 | 
//            homeServiceRefService.deleteByServiceId(homeServiceAddDTO.getId());  
 | 
//            addRef(homeServiceAddDTO,homeServiceAddDTO.getId());  
 | 
//        }  
 | 
  
 | 
    }  
 | 
  
 | 
    /**  
 | 
     * 将父级id更改为新的  
 | 
     * @param oldParentId  
 | 
     * @param newParentId  
 | 
     */  
 | 
    public void updateParentByParentId(Long oldParentId,Long newParentId){  
 | 
        Example example=new Example(HomeServiceInfo.class);  
 | 
        Example.Criteria criteria = example.createCriteria();  
 | 
        criteria.andEqualTo("parentId",oldParentId);  
 | 
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();  
 | 
        homeServiceInfo.setParentId(newParentId);  
 | 
        homeServiceInfoMapper.updateByExampleSelective(homeServiceInfo,example);  
 | 
    }  
 | 
  
 | 
  
 | 
    public HomeServiceInfo findSecondById(Long id){  
 | 
        HomeServiceInfo homeServiceInfo = homeServiceInfoMapper.selectByPrimaryKey(id);  
 | 
        List<PartnerFenceDTO> list=new ArrayList<>();  
 | 
        List<HomeServiceRef> byServiceId = homeServiceRefService.findByServiceId(id);  
 | 
        if(CollUtil.isNotEmpty(byServiceId)){  
 | 
            List<List<HomeServiceRef>> partnerId = CollUtil.groupByField(byServiceId, "partnerId");  
 | 
            for (List<HomeServiceRef> homeServiceRefs : partnerId) {  
 | 
                String partnerId1 = homeServiceRefs.get(0).getPartnerId();  
 | 
                PartnerFenceDTO partnerFenceDTO=new PartnerFenceDTO();  
 | 
                partnerFenceDTO.setPartnerId(partnerId1);  
 | 
                List<String> fenceIds=homeServiceRefs.stream().map(x->x.getFenceId()).collect(Collectors.toList());  
 | 
                partnerFenceDTO.setFenceIds(fenceIds);  
 | 
                list.add(partnerFenceDTO);  
 | 
            }  
 | 
            homeServiceInfo.setList(list);  
 | 
        }  
 | 
        return homeServiceInfo;  
 | 
    }  
 | 
  
 | 
  
 | 
    public List<HomeServiceInfo> findSecond(String partnerId, String fenceId){  
 | 
        return homeServiceInfoMapper.findSecond(partnerId,fenceId);  
 | 
    }  
 | 
}  
 |