From 8e93f63d7eab5c4f5d193763cbb2e81ea5476923 Mon Sep 17 00:00:00 2001
From: whywhyo <1511349576@qq.com>
Date: 星期日, 16 七月 2023 21:03:59 +0800
Subject: [PATCH] 559

---
 zhang-content/src/main/java/com/ruoyi/service/impl/ZfPetServiceImpl.java |  247 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 205 insertions(+), 42 deletions(-)

diff --git a/zhang-content/src/main/java/com/ruoyi/service/impl/ZfPetServiceImpl.java b/zhang-content/src/main/java/com/ruoyi/service/impl/ZfPetServiceImpl.java
index 5d1639f..1a6014a 100644
--- a/zhang-content/src/main/java/com/ruoyi/service/impl/ZfPetServiceImpl.java
+++ b/zhang-content/src/main/java/com/ruoyi/service/impl/ZfPetServiceImpl.java
@@ -9,13 +9,11 @@
 import com.ruoyi.common.utils.MapUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.common.utils.uuid.IdUtils;
-import com.ruoyi.domain.ZInfoUser;
-import com.ruoyi.domain.ZfMaster;
-import com.ruoyi.domain.ZfEquipment;
-import com.ruoyi.domain.ZfPet;
-import com.ruoyi.domain.ZfProperty;
+import com.ruoyi.domain.*;
 import com.ruoyi.mapper.ZfPetMapper;
+import com.ruoyi.service.ZAuthorityService;
 import com.ruoyi.service.ZInfoUserService;
 import com.ruoyi.service.ZfMasterService;
 import com.ruoyi.service.ZfPetService;
@@ -24,11 +22,16 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
+import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.ruoyi.constant.MenuAuthority.*;
 
 /**
  * <p>
@@ -50,23 +53,77 @@
 
     @Autowired
     ZfPetService zfPetService;
+    
+    @Resource
+    ZAuthorityService zAuthorityService;
     @Override
-    public AjaxResult selectPetList(ZfPet zfPet, Integer pageNum, Integer pageSize) {
-        LambdaQueryWrapper<ZfPet> lqw = buildCondition(zfPet);
-        lqw.orderByDesc(ZfPet::getCreateTime);
-        Page<ZfPet> ZfPetPage = new Page<>(pageNum,pageSize);
-        Page<ZfPet> pageResult = page(ZfPetPage, lqw);
-        HashMap<String, Object> data = MapUtils.getResult(pageResult);
+    public AjaxResult selectDataList(ZfPet zfPet, Integer pageNum, Integer pageSize) {
+//        LambdaQueryWrapper<ZfPet> lqw = buildCondition(zfPet);
+//        lqw.orderByDesc(ZfPet::getCreateTime);
+//        Page<ZfPet> ZfPetPage = new Page<>(pageNum,pageSize);
+//        Page<ZfPet> pageResult = page(ZfPetPage, lqw);
+//        HashMap<String, Object> data = MapUtils.getResult(pageResult);
+//        return AjaxResult.success(data);
+
+        //瑕佹煡鑷繁瀹跺涵鐨�
+        ZInfoUser myself = zInfoUserService.getMyself();
+        Long familyId = myself.getFamilyId();
+        //涔熻鏌ュ埆浜烘巿鏉冪殑
+        List<ZAuthority> authority = zAuthorityService.getAuthority();
+        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(FAMILY_PET_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
+        //鍔犱笂鑷繁瀹跺涵鐨刬d
+        idList.add(familyId);
+//        String familyIds = listFamilyIds();
+//        String secondFamilyAuthority = listSecondFamilyIds();
+        LambdaQueryWrapper<ZfPet> lqw = buildCondition(zfPet, idList);
+
+
+        Page<ZfPet> zfPetPage = new Page<>(pageNum, pageSize);
+        Page<ZfPet> pageResult = page(zfPetPage, lqw);
+
+        List<ZfPet> beanRecords = pageResult.getRecords();//寰楀埌鏌ヨ鍑烘潵鐨勬暟鎹�
+
+        List<ZfPet> dtoResult = markOwnData(familyId, beanRecords);
+
+        HashMap<String, Object> data = MapUtils.getResult(pageResult, dtoResult);
         return AjaxResult.success(data);
     }
 
+    public List<ZfPet> markOwnData(Long familyId,List<ZfPet> beanRecords){
+        return beanRecords.stream().peek(
+                bean -> {
+                    if (bean.getFamilyId() == familyId) {
+                        bean.setOwnData(1);
+                    } else {
+                        bean.setOwnData(0);
+                    }
+                }
+        ).collect(Collectors.toList());
+    }
+
+
+    
+
     @Override
     public List<ZfPet> selectByCondition(ZfPet zfPet) {
-        LambdaQueryWrapper<ZfPet> lambdaQueryWrapper = buildCondition(zfPet);
-        lambdaQueryWrapper.orderByDesc(ZfPet::getCreateTime);
-        List<ZfPet> list = list(lambdaQueryWrapper);
-        log.info("杩斿洖鐨勬暟鎹负:{}",list);
-        return list;
+//        LambdaQueryWrapper<ZfPet> lambdaQueryWrapper = buildCondition(zfPet);
+//        lambdaQueryWrapper.orderByDesc(ZfPet::getCreateTime);
+//        List<ZfPet> list = list(lambdaQueryWrapper);
+//        log.info("杩斿洖鐨勬暟鎹负:{}",list);
+//        return list;
+        //瑕佹煡鑷繁瀹跺涵鐨�
+        ZInfoUser myself = zInfoUserService.getMyself();
+        Long familyId = myself.getFamilyId();
+        //涔熻鏌ュ埆浜烘巿鏉冪殑
+        List<ZAuthority> authority = zAuthorityService.getAuthority();
+        List<Long> idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(FAMILY_PET_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
+        //鍔犱笂鑷繁瀹跺涵鐨刬d
+        idList.add(familyId);
+
+        LambdaQueryWrapper<ZfPet> lambdaQueryWrapper = buildCondition(zfPet, idList);
+        List<ZfPet> beanRecords = list(lambdaQueryWrapper);
+        log.info("浠庢暟鎹簱涓煡鍒扮殑涓�:{}", beanRecords);
+        return markOwnData(familyId, beanRecords);
     }
 
     @Override
@@ -80,44 +137,148 @@
     }
 
     @Override
-    @Transactional
-    public AjaxResult mySave(ZfPet zfPet) {
-        SysUser user = SecurityUtils.getLoginUser().getUser();
-        Long userId = user.getUserId();
+    public AjaxResult importExcel(MultipartFile file) {
+        ExcelUtil<ZfPet> util = new ExcelUtil<>(ZfPet.class);
+        List<ZfPet> dataList = null;
+        try {
+            dataList = util.importExcel(file.getInputStream());
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        log.info("鏁版嵁鍒楄〃涓猴細{}", dataList);
 
-        String pid = IdUtils.fastSimpleUUID();
-        zfPet.setId(pid);
+        for (ZfPet zfPet : dataList) {
+            zfPetService.addData(zfPet);
+        }
+
+        return AjaxResult.success("瀵煎叆鏁版嵁鎴愬姛");
+    }
+
+    @Override
+    public AjaxResult addData(ZfPet zfPet) {
+        ZInfoUser myself = zInfoUserService.getMyself();
+        Long familyId = myself.getFamilyId();
+
+        if(familyId == null){
+            throw new RuntimeException("鎮ㄨ繕鏈姞鍏ヤ换浣曞搴�");
+        }
+
+        List<ZAuthority> authority = zAuthorityService.getAuthority();
+        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(FAMILY_PET_ADD)).map(ZAuthority::getFid).collect(Collectors.toList());
+        familyIdList.add(familyId);
+
+        if (zfPet.getFamilyId()!=null && !familyIdList.contains(zfPet.getFamilyId())) {
+            throw new RuntimeException("浣犳病鏈夋潈闄愭搷浣滄瀹跺涵鐨勬暟鎹�");
+        }
+
+
+        if(zfPet.getFamilyId() == null){
+            //榛樿娣诲姞鑷繁瀹跺涵鐨勬暟鎹�
+            zfPet.setFamilyId(familyId);
+        }
 
         //鍒ゆ柇鏄惁鏈夐噸澶嶆暟鎹�
         LambdaQueryWrapper<ZfPet> lqw = uniqueCondition(zfPet);
         List<ZfPet> list = list(lqw);
+
         if(list.size()>0){
             throw new RuntimeException("璇峰嬁鏂板閲嶅鏁版嵁");
         }
 
-        //鍏堟煡鎵炬湰浜虹殑鏁版嵁
-        LambdaQueryWrapper<ZInfoUser> zInfoLqw = new LambdaQueryWrapper<>();
-        zInfoLqw.eq(ZInfoUser::getUserId,userId);
-        ZInfoUser master = zInfoUserService.getOne(zInfoLqw);
-
-        ZfMaster zfMaster = new ZfMaster();
-        zfMaster.setPetId(zfPet.getId());
-        zfMaster.setName(master.getNickName());
-        zfMaster.setCertificateType("韬唤璇�");
-        zfMaster.setCertificateNo(master.getIdNo());
-        zfMaster.setPhoneNo(master.getPhoneNumber());
-        zfMaster.setAddress(master.getAlwaysAddress());
-
-        zfMasterService.save(zfMaster);
-
-        zfPetService.save(zfPet);
-
-        return AjaxResult.success();
-
+        if (save(zfPet)) {
+            return AjaxResult.success();
+        } else {
+            return AjaxResult.error();
+        }
     }
+
+    @Override
+    public AjaxResult updateData(ZfPet zfPet) {
+        ZInfoUser myself = zInfoUserService.getMyself();
+        Long familyId = myself.getFamilyId();
+
+        //鍏堟牴鎹甶d鏌ヨ鍑烘暟鎹殑familyId锛岀湅鐪嬫湁娌℃湁鏉冮檺鎿嶄綔
+        Long dataFamilyId = getById(zfPet.getId()).getFamilyId();
+
+        List<ZAuthority> authority = zAuthorityService.getAuthority();
+        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(FAMILY_PET_UPDATE)).map(ZAuthority::getFid).collect(Collectors.toList());
+        familyIdList.add(familyId);
+
+        if (dataFamilyId!=null && !familyIdList.contains(dataFamilyId)) {
+            throw new RuntimeException("浣犳病鏈夋潈闄愭搷浣滄瀹跺涵鐨勬暟鎹�");
+        }
+
+        if(updateById(zfPet)){
+            return AjaxResult.success();
+        }else {
+            return AjaxResult.error();
+        }
+    }
+
+    @Override
+    public AjaxResult deleteData(Long[] ids) {
+        List<ZfPet> dataList = zfPetService.listByIds(Arrays.asList(ids));
+
+        ZInfoUser myself = zInfoUserService.getMyself();
+        Long familyId = myself.getFamilyId();
+
+        List<ZAuthority> authority = zAuthorityService.getAuthority();
+        List<Long> familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(FAMILY_PET_REMOVE)).map(ZAuthority::getFid).collect(Collectors.toList());
+        familyIdList.add(familyId);
+
+        for (ZfPet data : dataList) {
+            if (!familyIdList.contains(data.getFamilyId())){
+                throw new RuntimeException("浣犳病鏈夋潈闄愭搷浣滄瀹跺涵鐨勬暟鎹�");
+            }
+        }
+
+        if (zfPetService.removeByIds(Arrays.asList(ids))) {
+            return AjaxResult.success();
+        }else {
+            return AjaxResult.error();
+        }
+    }
+
+//    @Override
+//    @Transactional
+//    public AjaxResult mySave(ZfPet zfPet) {
+//        SysUser user = SecurityUtils.getLoginUser().getUser();
+//        Long userId = user.getUserId();
+//
+//        String pid = IdUtils.fastSimpleUUID();
+//        zfPet.setId(pid);
+//
+//        //鍒ゆ柇鏄惁鏈夐噸澶嶆暟鎹�
+//        LambdaQueryWrapper<ZfPet> lqw = uniqueCondition(zfPet);
+//        List<ZfPet> list = list(lqw);
+//        if(list.size()>0){
+//            throw new RuntimeException("璇峰嬁鏂板閲嶅鏁版嵁");
+//        }
+//
+//        //鍏堟煡鎵炬湰浜虹殑鏁版嵁
+//        LambdaQueryWrapper<ZInfoUser> zInfoLqw = new LambdaQueryWrapper<>();
+//        zInfoLqw.eq(ZInfoUser::getUserId,userId);
+//        ZInfoUser master = zInfoUserService.getOne(zInfoLqw);
+//
+//        ZfMaster zfMaster = new ZfMaster();
+//        zfMaster.setPetId(zfPet.getId());
+//        zfMaster.setName(master.getNickName());
+//        zfMaster.setCertificateType("韬唤璇�");
+//        zfMaster.setCertificateNo(master.getIdNo());
+//        zfMaster.setPhoneNo(master.getPhoneNumber());
+//        zfMaster.setAddress(master.getAlwaysAddress());
+//
+//        zfMasterService.save(zfMaster);
+//
+//        zfPetService.save(zfPet);
+//
+//        return AjaxResult.success();
+//
+//    }
 
     private LambdaQueryWrapper<ZfPet> uniqueCondition(ZfPet zfPet) {
         LambdaQueryWrapper<ZfPet> lqw = new LambdaQueryWrapper<>();
+        lqw.eq(zfPet.getFamilyId()!=null,ZfPet::getFamilyId,zfPet.getFamilyId());
         lqw.eq(StringUtils.isNotEmpty(zfPet.getIdNum()),ZfPet::getIdNum,zfPet.getIdNum());
         lqw.eq(StringUtils.isNotEmpty(zfPet.getSecurityCode()),ZfPet::getSecurityCode,zfPet.getSecurityCode());
         lqw.eq(StringUtils.isNotEmpty(zfPet.getType()),ZfPet::getType,zfPet.getType());
@@ -135,8 +296,10 @@
         return lqw;
     }
 
-    private LambdaQueryWrapper<ZfPet> buildCondition(ZfPet zfPet) {
+    private LambdaQueryWrapper<ZfPet> buildCondition(ZfPet zfPet,List<Long> familyIdList) {
         LambdaQueryWrapper<ZfPet> lqw = new LambdaQueryWrapper<>();
+        lqw.orderByDesc(ZfPet::getCreateTime);
+        lqw.in(ZfPet::getFamilyId,familyIdList);
         lqw.like(StringUtils.isNotEmpty(zfPet.getIdNum()),ZfPet::getIdNum,zfPet.getIdNum());
         lqw.like(StringUtils.isNotEmpty(zfPet.getSecurityCode()),ZfPet::getSecurityCode,zfPet.getSecurityCode());
         lqw.like(StringUtils.isNotEmpty(zfPet.getType()),ZfPet::getType,zfPet.getType());

--
Gitblit v1.9.1