songhuangfeng123
2022-08-19 759bbb8ee99665382a689fa90277a9b2279238da
emergency/emergency-service/src/main/java/com/gkhy/safePlatform/emergency/service/impl/EmergencyCountServiceImpl.java
对比新文件
@@ -0,0 +1,152 @@
package com.gkhy.safePlatform.emergency.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.emergency.entity.*;
import com.gkhy.safePlatform.emergency.model.dto.resp.*;
import com.gkhy.safePlatform.emergency.query.EmergencyDrillExecuteCountQuery;
import com.gkhy.safePlatform.emergency.query.EmergencySuppliesCountQuery;
import com.gkhy.safePlatform.emergency.service.EmergencyCountService;
import com.gkhy.safePlatform.emergency.service.EmergencySuppliesService;
import com.gkhy.safePlatform.emergency.service.baseService.EmergencyDrillExecuteInfoService;
import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInfoService;
import com.gkhy.safePlatform.emergency.utils.TimeUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.text.SimpleDateFormat;
import java.util.*;
@Service("emergencyCountService")
public class EmergencyCountServiceImpl implements EmergencyCountService {
    @Autowired
    private EmergencyDrillExecuteInfoService emergencyDrillExecuteInfoService;
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
    @Autowired
    private EmergencySuppliesInfoService emergencySuppliesInfoService;
    @Override
    public ResultVO<EmergencyDrillExecuteCountRespDTO> countEmergencyDrillExecute(EmergencyDrillExecuteCountQuery query) {
        EmergencyDrillExecuteCountRespDTO emergencyDrillExecuteCountRespDTO = new EmergencyDrillExecuteCountRespDTO();
        // 根据最新的应急演练实施的创建时间与当前时间  获取间隔天数
        Integer days = emergencyDrillExecuteInfoService.selectEmergencyDrillExecuteIntervalTime();
        emergencyDrillExecuteCountRespDTO.setDays(days);
        // 根据时间类型(年/月),部门id 获取统计结果
        List<EmergencyDrillExecuteCountData> list = new ArrayList<>();
        // 获取部门id集合
        List<Long> deptIds = new ArrayList<>();
        ResultVO<List<Long>> rpcResult = accountDepartmentService.listDepAndSubDepIds(query.getDeptId());
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                deptIds = (List<Long>) rpcResult.getData();
            }
        }else{
            throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
        }
        // 获取时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        int year = Integer.parseInt(sdf.format(date));
        if (query.getType()==1){
            // 月
            String startTime = TimeUtils.getYearFirst(year);
            String endTime = TimeUtils.getYearLast(year);
            list = emergencyDrillExecuteInfoService.selectByMonthAndDept(startTime,endTime,deptIds);
        }
        if (query.getType()==2){
            // 年
            String startTime = TimeUtils.getYearFirst(year-10);
            String endTime = TimeUtils.getYearLast(year);
            list = emergencyDrillExecuteInfoService.selectByYearAndDept(startTime,endTime,deptIds);
        }
        if (!CollectionUtils.isEmpty(list)){
            List<EmergencyDrillExecuteCountDataRespDTO> dataList = BeanCopyUtils.copyBeanList(list,EmergencyDrillExecuteCountDataRespDTO.class);
            emergencyDrillExecuteCountRespDTO.setDataList(dataList);
        }else{
            emergencyDrillExecuteCountRespDTO.setDataList(new ArrayList<>());
        }
        return new ResultVO<>(ResultCodes.OK,emergencyDrillExecuteCountRespDTO);
    }
    @Override
    public ResultVO<List<EmergencySuppliesCountRespDTO>> countEmergencySupplies(PageQuery<EmergencySuppliesCountQuery> pageQuery) {
        List<EmergencySuppliesCountRespDTO> respList = new ArrayList<>();
        Long pageIndex = pageQuery.getPageIndex();
        Long pageSize = pageQuery.getPageSize();
        Page<EmergencySuppliesInfoDetailDO> page = new Page<>(pageIndex,pageSize);
        List<EmergencySuppliesInfoDetailDO> emergencySuppliesInfoDetailDOList =  emergencySuppliesInfoService.countEmergencySupplies(page,pageQuery.getSearchParams());
        if (!CollectionUtils.isEmpty(emergencySuppliesInfoDetailDOList)){
            for (EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO :emergencySuppliesInfoDetailDOList){
                EmergencySuppliesCountRespDTO emergencySuppliesCountRespDTO = new EmergencySuppliesCountRespDTO();
                // 名称+数量
                emergencySuppliesCountRespDTO.setName(emergencySuppliesInfoDetailDO.getName());
                emergencySuppliesCountRespDTO.setTotalNum(emergencySuppliesInfoDetailDO.getCount());
//                // 保质期内数量
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化一下
//                Date produceDate = emergencySuppliesInfoDetailDO.getProductionDate();
//                Integer usePeriod = emergencySuppliesInfoDetailDO.getUsePeriod();
//
//                String[] arr = sdf.format(produceDate).split("-");
//
//                Calendar calendar = Calendar.getInstance();//获取对日期操作的类对象
//                calendar.clear();
//                calendar.set(Calendar.YEAR, Integer.parseInt(arr[0]));
//                calendar.set(Calendar.MONTH, Integer.parseInt(arr[1]));
//                calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(arr[2]));
//
//                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) +usePeriod);
//
//                Date qualityD = calendar.getTime();
//                String time= sdf.format(calendar.getTime());
//                System.out.println(time);
                respList.add(emergencySuppliesCountRespDTO);
            }
        }
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
    public static void main(String[] args) {
         Date date = new Date();//获取当前日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化一下
         Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象
         //两种写法都可以获取到前三天的日期
          calendar1.set(Calendar.DAY_OF_YEAR,calendar1.get(Calendar.DAY_OF_YEAR) -40);
         //在当前时间的基础上获取前三天的日期
//         calendar1.add(Calendar.DATE, -40);
        //add方法 参数也可传入 月份,获取的是前几月或后几月的日期
        //calendar1.add(Calendar.MONTH, -3);
         Date today = calendar1.getTime();
         String time= sdf.format(today);
        System.out.println(time);
    }
}