Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / River / PatrolManageProblemController.java
package com.newfiber.api.pc.controller.River;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.core.annotation.SysLog;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.core.support.HttpCode;
import com.newfiber.api.core.utils.Constant;
import com.newfiber.api.core.utils.DateUtil;
import com.newfiber.api.core.utils.ExportExcel;
import com.newfiber.api.pc.model.River.PatrolManageProblem;
import com.newfiber.api.pc.model.River.PatrolManageProblemDto;
import com.newfiber.api.pc.model.River.ProblemRankDto;
import com.newfiber.api.pc.service.PatrolManageDicService;
import com.newfiber.api.pc.service.PatrolManageProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.*;


/**
 *  patrolManageProblem控制层
 *
 * @author luzhan
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/problem")
@Api(value = "API - PatrolManageProblemController", description = "巡查管理问题")
public class PatrolManageProblemController {

    private static final Logger log = LoggerFactory.getLogger(PatrolManageProblemController.class);

    @Autowired
    public PatrolManageProblemService patrolManageProblemService;

    @Autowired
    private PatrolManageDicService patrolManageDicService;

    /**
     * 获取patrolManageProblem信息(分页)
     * @param params
     * @return
     */
    @PostMapping("list")
    @ApiOperation(value = "获取patrol_manage_problem列表",notes="")
    @SysLog(value="获取patrol_manage_problem列表",actionType="4")
    public RespBodyObj<PageInfo<PatrolManageProblem>> list(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        Integer pageNo = params.getCurrent();
        Integer pageSize = params.getSize();
        String searchStr = String.valueOf(params.getData().get("searchStr" ));
        PageInfo<PatrolManageProblem> patrolManageProblem =patrolManageProblemService.listForPage(searchStr, pageNo, pageSize);
        return RespBodyObj.ok(patrolManageProblem);
    }

    @PostMapping("stateList")
    @ApiOperation(value = "获取patrol_manage_problem列表",notes="")
    @SysLog(value="获取patrol_manage_problem列表",actionType="4")
    public RespBodyObj stateList(@RequestBody ReqBodyObj<Map<String, Object>> params,HttpServletRequest request) {
        Integer pageNo = params.getCurrent();
        Integer pageSize = params.getSize();
        //patrolType 1河道/2管网,state 提交来源,problemState 待处理/已完成
        Integer patrolType = StringUtils.isEmpty(params.getData().get("patrolType").toString())?null:Integer.valueOf(params.getData().get("patrolType").toString());
        Integer state = StringUtils.isEmpty(params.getData().get("state").toString())?null:Integer.valueOf(params.getData().get("state").toString());
        String problemState = String.valueOf(params.getData().get("problemState"));
        String searchStr = StringUtils.isEmpty(params.getData().get("searchStr").toString())?null:params.getData().get("searchStr").toString();
        Integer stateType = StringUtils.isEmpty(params.getData().get("stateType").toString())?null:Integer.valueOf(params.getData().get("stateType").toString());
        //projectIds 河道或管网的id,problemIds问题类型id
        List<Long> pipe = StringUtils.isEmpty(params.getData().get("project").toString())?null:(List<Long>) params.getData().get("project");
        Set<Long> projectIds = new HashSet<>();
        projectIds.addAll(pipe);
        Long riverProblemType = params.getData().get("riverProblemType")==null?null:StringUtils.isEmpty(params.getData().get("riverProblemType").toString())?null:Long.valueOf(params.getData().get("riverProblemType").toString());
        Long pipeProblemType = params.getData().get("pipeProblemType")==null?null:StringUtils.isEmpty(params.getData().get("pipeProblemType").toString())?null:Long.valueOf(params.getData().get("pipeProblemType").toString());
        Set<Long> problemIds = new HashSet<>();
        if(riverProblemType!=null){
            problemIds.add(riverProblemType);
        }
        if(pipeProblemType!=null){
            problemIds.add(pipeProblemType);
        }

        //userName 用户名
        String userName = StringUtils.isEmpty(params.getData().get("userName").toString())?null:params.getData().get("userName").toString();
        String startTime = org.springframework.util.StringUtils.isEmpty(params.getData().get("startTime"))?null:params.getData().get("startTime").toString();
        String endTime = org.springframework.util.StringUtils.isEmpty(params.getData().get("endTime"))?null:params.getData().get("endTime").toString();

        Map<String,Object> patrolManageProblem =patrolManageProblemService.patrolProblemList(request.getHeader("userNo"),request.getHeader("highest"),patrolType,problemState,state,stateType,projectIds,problemIds,userName,searchStr,startTime,endTime, pageNo, pageSize);
        return RespBodyObj.ok(patrolManageProblem);
    }

    /**
     * 获取查询条件
     * @param params
     * @param request
     * @return
     */
    @PostMapping("getCondition")
    @ApiOperation(value = "获取patrol_manage_problem列表",notes="")
    @SysLog(value="获取patrol_manage_problem列表",actionType="4")
    public RespBodyObj getCondition(@RequestBody ReqBodyObj<Map<String, Object>> params,HttpServletRequest request) {
        Integer patrolType = StringUtils.isEmpty(params.getData().get("patrolType").toString())?null:Integer.valueOf(params.getData().get("patrolType").toString());
        Map<String,Object> patrolManageProblem =patrolManageProblemService.getCondition(patrolType);
        return RespBodyObj.ok(patrolManageProblem);
    }

    /**
     * pc问题列表
     * @param params
     * @param request
     * @return
     */
    @PostMapping("problemPcList")
    @ApiOperation(value = "获取patrol_manage_problem列表",notes="")
    @SysLog(value="获取pc问题列表",actionType="4")
    public RespBodyObj<List<PatrolManageProblemDto>> problemPcList(@RequestBody ReqBodyObj<Map<String, Object>> params,HttpServletRequest request) {
        Long patrolNo = Long.valueOf(params.getData().get("patrolNo").toString());
        List<PatrolManageProblemDto> patrolManageProblem =patrolManageProblemService.pcProblemList(patrolNo);
        return RespBodyObj.ok(patrolManageProblem);
    }

    /**
     * pc问题详情
     * @param params
     * @param request
     * @return
     */
    @PostMapping("pcDetail")
    @ApiOperation(value = "获取patrol_manage_problem列表",notes="")
    @SysLog(value="pc问题详情",actionType="4")
    public RespBodyObj<PatrolManageProblemDto> pcDetail(@RequestBody ReqBodyObj<Map<String, Object>> params,HttpServletRequest request) {
        Long problemId = Long.valueOf(params.getData().get("problemId").toString());
        PatrolManageProblemDto patrolManageProblem =patrolManageProblemService.pcProblemDetail(problemId,request.getHeader("userNo"),request.getHeader("highest"));
        return RespBodyObj.ok(patrolManageProblem);
    }

    /**
     * 系统报警提交
     * @param params
     * @return
     */
    @PostMapping("dataError")
    @ApiOperation(value = "新增或编辑patrol_manage_problem",notes="根据实体修改patrol_manage_problem")
    @SysLog(value="patrolManageProblem添加或编辑",actionType="1")
    public RespBodyObj dataError(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        log.info("历史报警insertBatch"+params.toString());
        if(!StringUtils.isEmpty(params.getData().get("problemErr").toString())){
            String err = JSON.toJSONString(params.getData().get("problemErr"));
            List<PatrolManageProblem> errs = JSONObject.parseArray(err, PatrolManageProblem.class);
            patrolManageProblemService.dataError(errs);
            return RespBodyObj.ok();
        }
        return RespBodyObj.error();
    }

    /**
     * patrolManageProblem添加或编辑
     * @param param
     * @return
     */
    @PostMapping("save")
    @ApiOperation(value = "新增或编辑patrol_manage_problem",notes="根据实体修改patrol_manage_problem")
    @SysLog(value="patrolManageProblem添加或编辑",actionType="1")
    public RespBodyObj save(@RequestBody ReqBodyObj<PatrolManageProblem> param, HttpServletRequest request) {
        try {
            //校验参数 todo
            if (validateParam(param)) return RespBodyObj.error("巡查项目不能为空!");
            param.getData().setCreateUserNo(request.getHeader("userNo"));
            return patrolManageProblemService.save(param.getData());
        } catch (Exception e) {
            log.error("/patrolManageProblem/saveOrupdate", e);
            return RespBodyObj.error(Constant.ERROR);
        }
    }

    /**
     * 校验参数
     * @param param
     * @return
     */
    private boolean validateParam(@RequestBody ReqBodyObj<PatrolManageProblem> param) {
        if(param.getData()!=null && param.getData().getSourceType()!=null
                &&(param.getData().getSourceType() == 4 || param.getData().getSourceType() == 5)){
            if(param.getData().getProjectMainId() == null || StringUtils.isEmpty(param.getData().getProjectMainName())){
                return true;
            }
        }
        return false;
    }

    /**
     * patrolManageProblem批量移除
     *
     * @param params
     * @return
     */
    @PostMapping("removeBatch")
    @ApiOperation(value = "批量删除辑patrol_manage_problem",notes="根据id批量删除patrol_manage_problem")
    @SysLog(value="patrolManageProblem批量移除",actionType="2")
    public RespBodyObj removeBatch(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        try {
            List<Long> list = (List<Long>) params.getData().get("ids");
            boolean b = patrolManageProblemService.deleteBatchIds(list);
            return b ? RespBodyObj.ok(Constant.SUCCESS) : RespBodyObj.error(Constant.FAILED);
        } catch (Exception e) {
            log.error("/patrolManageProblem/removeBatch", e);
            return RespBodyObj.error(Constant.ERROR);
        }
    }

   /**
   * patrolManageProblem查询单个
   *
   * @param params
   * @return
   */
    @PostMapping("selectOne")
    @ApiOperation(value = "查询patrol_manage_problem",notes="根据主键查询patrol_manage_problem")
    @SysLog(value="patrolManageProblem查询单个",actionType="4")
    public RespBodyObj<PatrolManageProblem> selectOne(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        if (params.getData() == null) {
            return RespBodyObj.error(HttpCode.BAD_REQUEST);
        }
        if (StringUtils.isEmpty(params.getData().get("id" ).toString())) {
            return RespBodyObj.error(HttpCode.BAD_REQUEST);
        }
        String id = params.getData().get("id" ).toString();
        PatrolManageProblem patrolManageProblem = patrolManageProblemService.selectById(    id);
        return RespBodyObj.ok(patrolManageProblem);
    }

    /**
     * patrolManageProblem查询单个
     *
     * @return
     */
    @PostMapping("rankList")
    @ApiOperation(value = "查询巡查的排行榜",notes="查询巡查的排行榜")
    @SysLog(value="查询巡查的排行榜",actionType="4")
    public RespBodyObj<List<ProblemRankDto>> rankList() {
        List<ProblemRankDto> problemRankDto = patrolManageProblemService.rankList();
        return RespBodyObj.ok(problemRankDto);
    }

    /**
     * 导出功能
     * @param params
     * @param out
     * @param response
     */
    @ApiOperation("excel导出")
    @PostMapping(value = "excelExport", produces = "application/json;charset=utf-8")
    public void excelExport(@RequestBody ReqBodyObj<Map<String, Object>> params, OutputStream out, HttpServletResponse response,HttpServletRequest request) {
        Integer pageNo = params.getCurrent();
        Integer pageSize = params.getSize();
        //patrolType 1河道/2管网,state 提交来源,problemState 待处理/已完成
        Integer patrolType = StringUtils.isEmpty(params.getData().get("patrolType").toString())?null:Integer.valueOf(params.getData().get("patrolType").toString());
        Integer state = StringUtils.isEmpty(params.getData().get("state").toString())?null:Integer.valueOf(params.getData().get("state").toString());
        String problemState = String.valueOf(params.getData().get("problemState"));
        String searchStr = StringUtils.isEmpty(params.getData().get("searchStr").toString())?null:params.getData().get("searchStr").toString();
        Integer stateType = StringUtils.isEmpty(params.getData().get("stateType").toString())?null:Integer.valueOf(params.getData().get("stateType").toString());
        //projectIds 河道或管网的id,problemIds问题类型id
        List<Long> pipe = StringUtils.isEmpty(params.getData().get("project").toString())?null:(List<Long>) params.getData().get("project");
        Set<Long> projectIds = new HashSet<>();
        projectIds.addAll(pipe);
        Long pipeProblemType = StringUtils.isEmpty(params.getData().get("problemType").toString())?null:Long.valueOf(params.getData().get("problemType").toString());
        Set<Long> problemIds = new HashSet<>();
        if(pipeProblemType!=null){
            problemIds.add(pipeProblemType);
        }

        //userName 用户名
        String userName = StringUtils.isEmpty(params.getData().get("userName").toString())?null:params.getData().get("userName").toString();
        String startTime = org.springframework.util.StringUtils.isEmpty(params.getData().get("startTime"))?null:params.getData().get("startTime").toString();
        String endTime = org.springframework.util.StringUtils.isEmpty(params.getData().get("endTime"))?null:params.getData().get("endTime").toString();

        Map<String,Object> patrolManageProblem =patrolManageProblemService.patrolExcelList(request.getHeader("userNo"),request.getHeader("highest"),patrolType,problemState,state,stateType,projectIds,problemIds,userName,searchStr,startTime,endTime, null, null);
        List<PatrolManageProblemDto> list = castList(patrolManageProblem.get("rows"), PatrolManageProblemDto.class);
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<String> headers = new ArrayList<>();
        headers.add("问题类别");
        headers.add("巡查编号");
        headers.add("巡查记录名称");
        headers.add("巡查名称");
        headers.add("所在地点");
        headers.add("问题类型");
        headers.add("问题地址");
        headers.add("事件描述");
        headers.add("经纬度");
        headers.add("问题状态");
        headers.add("创建者");
        headers.add("数据来源");
        headers.add("上报时间");
        headers.add("修改状态时间");
        try {
            List<List> result = new ArrayList<>();
            for (PatrolManageProblemDto dto : list) {
                List<String> ex = new ArrayList<>();
                if(dto.getPatrolType() == null){
                    ex.add("-");
                }else{
                    ex.add(getPatrolType(dto.getPatrolType()));
                }
                ex.add(dto.getPatrolNo()==null?"-":dto.getPatrolNo().toString());
                ex.add(StringUtils.isEmpty(dto.getPatrolName())?"-":dto.getPatrolName().toString());
                ex.add(StringUtils.isEmpty(dto.getProblemName())?"-":dto.getProblemName().toString());
                ex.add(StringUtils.isEmpty(dto.getProjectMainName())?"-":dto.getProjectMainName().toString());
                ex.add(StringUtils.isEmpty(dto.getProblemTypeName())?"-":dto.getProblemTypeName());
                ex.add(StringUtils.isEmpty(dto.getAddress())?"-":dto.getAddress());
                ex.add(StringUtils.isEmpty(dto.getDescription())?"-":dto.getDescription());
                ex.add(patrolManageProblemService.transitionJsonObjct(dto.getCoordinate()));
                ex.add(StringUtils.isEmpty(dto.getStateDesc())?"-":dto.getStateDesc());
                ex.add(StringUtils.isEmpty(dto.getCreateUserName())?"-":dto.getCreateUserName());
                if(dto.getSubSource() == null){
                    ex.add("其他");
                }else{
                    ex.add(getSourceType(dto.getSubSource()));
                }
                ex.add(DateUtil.getSdfDate(dto.getCreateTime()));
                ex.add(DateUtil.getSdfDate(dto.getUpdateTime()));
                result.add(ex);
            }
            ExportExcel excel = new ExportExcel();
            StringBuffer sb = new StringBuffer();
            sb.append("智慧水务平台接收事件表-");
            String stringDate = DateUtil.getStringDate();
            sb.append(stringDate);
            excel.exportExcel3(sb.toString(), headers, result, out, response,(new Date().toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getSourceType(int type){
        String typeName = "";
        // 平台预警 5
        // 领导交办 2
        // 数字城管 1
        // 日常巡查 4
        // 数管热线(12319) 7
        // 市长热线(12345) 6
        // 水务热线(6381234) 3
        // 其他 0
        switch(type){
            case 8:
                typeName = "其他";
                break;
            case 1:
                typeName = "数字城管";
                break;
            case 2:
                typeName = "领导交办";
                break;
            case 3:
                typeName = "水务热线(6381234)";
                break;
            case 4:
                typeName = "日常巡查";
                break;
            case 5:
                typeName = "平台预警";
                break;
            case 6:
                typeName = "市长热线(12345)";
                break;
            case 7:
                typeName = "数管热线(12319)";
                break;
            default:
                typeName = "其他";
        }
        return typeName;
    }

    private String getPatrolType(int type){
        /*
        1 河道
        2 管网
        3 城市防汛
        4 供水保障
        5 污水处理
        6 工程
         */
        String typeName = "";
        switch(type){
            case 1:
                typeName = "河道巡查";
                break;
            case 2:
                typeName = "管网巡查";
                break;
            case 3:
                typeName = "城市防汛巡查";
                break;
            case 4:
                typeName = "供水保障巡查";
                break;
            case 5:
                typeName = "污水处理巡查";
                break;
            case 6:
                typeName = "工程巡查";
                break;
            default:
                typeName = "其他巡查";
        }
        return typeName;
    }

    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
}