ProcessStatisticsController.java 11.6 KB
package com.skua.modules.process.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.core.api.ISysBaseAPI;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.context.BaseContextHandler;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.DateUtils;
import com.skua.modules.equipment.entity.EquipmentMaintainTask;
import com.skua.modules.equipment.service.IEquipmentMaintainTaskService;

import com.skua.modules.flow.business.service.FlowBusinessService;
import com.skua.modules.flow.core.entity.BladeFlow;
import com.skua.modules.flow.service.FlowForThirdBusinessService;
import com.skua.modules.inspection.entity.InspectionUserWorkingGroup;
import com.skua.modules.inspection.service.IInspectionTaskService;
import com.skua.modules.inspection.service.IInspectionUserWorkingGroupService;
import com.skua.modules.process.vo.ProcessStatisticsVO;
import com.skua.modules.process.vo.ThirdProcessVO;
import com.skua.modules.quartz.util.BaseUtil;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.entity.SysUserRole;
import com.skua.modules.system.service.ISysUserRoleService;
import com.skua.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Api(tags="测试流程")
@RestController("testProcessController")
@RequestMapping("/web/process")
public class ProcessStatisticsController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private FlowBusinessService flowBusinessService;
    @Autowired
    private FlowForThirdBusinessService flowForThirdBusinessService;
    @Autowired
    private IEquipmentMaintainTaskService equipMaintainExecuteService;
    @Autowired
    private IInspectionTaskService inspectionTaskService;
    @Autowired
    private IInspectionUserWorkingGroupService inspectionUserWorkingGroupService;

    /*      问题上报流程	problemReport
            设备维修流程	EquipmentRepair
            设备缺陷管理	EquipmentDefectManagement
            事故事件流程	EmergencyRiskEvent
            生产任务流程	productive_task
            数据报警工单	productAlarmProcess
            安全隐患流程	AjhRectification
            考核评价流程	dataAssessmentSocreMaster
            危险作业流程	DangerousOperationManage*/
    @AutoLog(value = "测试流程"  )
    @ApiOperation(value="测试流程-统计", notes="测试流程-统计")
    @GetMapping(value = "/statistics")
    public Result<List<ProcessStatisticsVO>> statistics() {
        Result<List<ProcessStatisticsVO>> result = new Result<List<ProcessStatisticsVO>>();
        List<ProcessStatisticsVO>  processStatisticsList = new ArrayList<>();
        String userId = BaseContextHandler.getUserId();
        IPage<BladeFlow> page = new Page<BladeFlow>();
        page.setCurrent(1);
        page.setSize(10000);
        BladeFlow bladeFlow = new BladeFlow();
        try {
            //巡检任务、维修任务、保养任务、危险作业、事故事件、安全隐患、、药剂入库
            String process_key = "AjhRectification";
            processStatisticsList.add( queryProcessStatistics("安全隐患",  process_key,page,bladeFlow) ) ;
            process_key = "problemReport";
            processStatisticsList.add(  queryProcessStatistics("问题上报",  process_key,page,bladeFlow)) ;
            process_key = "EmergencyRiskEvent";
            processStatisticsList.add( queryProcessStatistics("事故事件",  process_key,page,bladeFlow) ) ;
            process_key = "DangerousOperationManage";
            processStatisticsList.add( queryProcessStatistics("危险作业",  process_key,page,bladeFlow) ) ;
            process_key = "EquipmentRepair";
            processStatisticsList.add( queryProcessStatistics("维修任务",  process_key,page,bladeFlow)) ;

            Integer baoyangTodoCount = 0;
            QueryWrapper equipmentMaintainTaskQueryWrapper = new QueryWrapper<EquipmentMaintainTask>();
            equipmentMaintainTaskQueryWrapper.eq("maintainer_id", userId);
            //equipmentMaintainTaskQueryWrapper.eq("results_enforcement", 1);
            baoyangTodoCount = equipMaintainExecuteService.count(equipmentMaintainTaskQueryWrapper);
            ProcessStatisticsVO   maintainerProcessStatisticsVO =   new ProcessStatisticsVO("保养任务" ,""  , "0", baoyangTodoCount.toString() );
            processStatisticsList.add(  maintainerProcessStatisticsVO );

            //巡检
            //得到巡检任务
            //得到进行中的任务
            //获取用户所在的班组
            Integer xunjian_count = 0;
            QueryWrapper<InspectionUserWorkingGroup> inspectionUserWorkingGroupQueryWrapper = new QueryWrapper<>();
            inspectionUserWorkingGroupQueryWrapper.eq("user_id", userId);
            List<InspectionUserWorkingGroup> inspectionUserWorkingGroupList = inspectionUserWorkingGroupService.list(inspectionUserWorkingGroupQueryWrapper);

            List<String> inspectionWorkingGroupIdList = new ArrayList<>();
            for (InspectionUserWorkingGroup inspectionUserWorkingGroup : inspectionUserWorkingGroupList) {
                inspectionWorkingGroupIdList.add(inspectionUserWorkingGroup.getInspectionWorkingGroupId());
            }
            if (inspectionWorkingGroupIdList.size() > 0) {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("task_state", "1");
                queryWrapper.in("inspection_working_group_id", inspectionWorkingGroupIdList);
                xunjian_count = inspectionTaskService.count(queryWrapper);
            } else {
                xunjian_count = 0;
            }

            //得到当前未开始的
            String moduleType = "1";
            List<JSONObject> inspectionTaskByUserIdList = inspectionTaskService.getInspectionTaskByUserId(userId, moduleType);
            ProcessStatisticsVO   xunjianProcessStatisticsVO =   new ProcessStatisticsVO("巡检任务" ,""  , "0", inspectionTaskByUserIdList.size()+"" );

            processStatisticsList.add(  xunjianProcessStatisticsVO );

            //药剂入库
            String userRoleIds = BaseContextHandler.getRoles();
            String roleIds = BaseUtil.quoteEach(userRoleIds,",");


            JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
            Integer materialINCount = 0;
            Integer  authFlag = 0;
            //如果为管理员,则跳过权限
            if ("1".equals(BaseContextHandler.get("userType"))) {
                authFlag  = 1;
            }else{
                StringBuffer sql  = new StringBuffer("   select count(1) from sys_role_permission   rp  ,sys_config c ");
                sql.append("   where rp.permission_id = c.config_value  and c.config_id = 'MaterialIN_ID' and rp.role_id in ("+roleIds+") "  );
                 authFlag =    masterDB.queryForObject(sql.toString(),Integer.class);
            }
            if(authFlag.intValue() > 0){
                String sql2 = "select count(1) from erp_material_in where confirm_status =2 and audit_status>0 and audit_status<3 and apply_time >= '"+BaseUtil.getLastYearDate()+"' and apply_time<='"+BaseUtil.getCurrentDate()+"' ";
                materialINCount =    masterDB.queryForObject(sql2,Integer.class);
            }

            ProcessStatisticsVO   materialINProcessStatisticsVO =   new ProcessStatisticsVO("药剂入库" ,""  , "0",materialINCount.toString() );
            processStatisticsList.add(  materialINProcessStatisticsVO );

            result.setResult( processStatisticsList );
            result.success("获取统计数据");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    private ProcessStatisticsVO queryProcessStatistics(String processName, String process_key, IPage<BladeFlow> page,BladeFlow bladeFlow){
       // String repairProcessDefinitionKey = "equipServicewx";
        bladeFlow.setProcessDefinitionKey(process_key);
        long repairClaimCount = flowBusinessService.selectClaimPage(page, bladeFlow).getTotal();
        long repairToDoCount = flowBusinessService.selectTodoPage(page, bladeFlow).getTotal();
       // long repairDoneCount = flowBusinessService.selectDonePage(page, bladeFlow).getTotal();

        return  new ProcessStatisticsVO(processName ,process_key  , repairToDoCount+"", repairClaimCount+"" );
    }

    @AutoLog(value = "第三方获取待办流程"  )
    @ApiOperation(value="第三方获取待办流程", notes="第三方获取待办流程")
    @GetMapping(value = "/getProcessByPhone")
    public Result<Map<String,Object>> getProcessByPhone(String phone) {
        Result<Map<String,Object>> result = new Result<Map<String,Object>>();
        Map<String,Object> map = new HashMap<>();
        List<ThirdProcessVO> list = new ArrayList<>();
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        if(sysUser!=null){
            String userName = sysUser.getRealname();
            BladeFlow bladeFlow = new BladeFlow();
            List<BladeFlow> claimList = new ArrayList<>();
            List<BladeFlow> todoList = new ArrayList<>();
            List<String> roleList = sysBaseAPI.getRolesByUserId(sysUser.getId());
            String roles = roleList.stream().map(s -> s.trim()).collect(Collectors.joining(","));
            if(roleList.size() > 0){
                claimList = flowForThirdBusinessService.selectClaimList(bladeFlow, sysUser.getId(), roles);
                for (BladeFlow claim : claimList) {
                    ThirdProcessVO claimVO = new ThirdProcessVO();
                    claimVO.setMsgid(claim.getTaskId());
                    claimVO.setMessagenote(claim.getBusinessDesc());
                    if(claim.getCreateTime()!=null){
                        claimVO.setSenddate(DateUtils.format(new Date(), "YYYY-MM-dd"));
                    }
                    claimVO.setCheckcode(phone);
                    claimVO.setCheckname(userName);
                    claimVO.setSendercode(phone);
                    claimVO.setSendername(userName);
                    list.add(claimVO);
                }
            }
            todoList = flowForThirdBusinessService.selectTodoList(bladeFlow, sysUser.getId());
            for (BladeFlow todo : todoList) {
                ThirdProcessVO todoVO = new ThirdProcessVO();
                todoVO.setMsgid(todo.getTaskId());
                todoVO.setMessagenote(todo.getBusinessDesc());
                if(todo.getCreateTime()!=null){
                    todoVO.setSenddate(DateUtils.format(new Date(), "YYYY-MM-dd"));
                }
                todoVO.setCheckcode(phone);
                todoVO.setCheckname(userName);
                todoVO.setSendercode(phone);
                todoVO.setSendername(userName);
                list.add(todoVO);
            }
        }
        map.put("DATAS",list);
        map.put("COUNT",list.size());
        result.setResult(map);
        return result;
    }
}