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


import com.github.pagehelper.PageInfo;
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.ExportExcel;
import com.newfiber.api.pc.dao.RiverManage.PatrolManageStatisticsMapper;
import com.newfiber.api.pc.dao.RiverManage.PatrolManageUserRelationMapper;
import com.newfiber.api.pc.model.River.PatrolManageStatistics;
import com.newfiber.api.pc.model.River.PatrolManageUser;
import com.newfiber.api.pc.model.River.PatrolManageUserData;
import com.newfiber.api.pc.model.River.PatrolManageUserRelation;
import com.newfiber.api.pc.service.PatrolManageStatisticsService;
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.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *  patrolManageStatistics控制层
 *
 * @author luzhan
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/patrolManageStatistics")
@Api(value = "API - PatrolManageStatisticsController", description = "")
public class PatrolManageStatisticsController {

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

    @Autowired
    public PatrolManageStatisticsService patrolManageStatisticsService;

    @Autowired
    private PatrolManageUserRelationMapper patrolManageUserRelationMapper;

    @Autowired
    private PatrolManageStatisticsMapper patrolManageStatisticsMapper;

    @PostMapping("getList")
    @ApiOperation(value = "获取达标率",notes="")
    public RespBodyObj<PageInfo<PatrolManageUser>> getList(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        Integer pageNo = params.getCurrent();
        Integer pageSize = params.getSize();
        String userName = org.springframework.util.StringUtils.isEmpty(params.getData().get("userName"))?null:params.getData().get("userName").toString();
        Integer patrolType = org.springframework.util.StringUtils.isEmpty(params.getData().get("patrolType"))?null:Integer.valueOf(params.getData().get("patrolType").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();
        PageInfo<PatrolManageUser> patrolManageStatistics =patrolManageStatisticsService.getList(userName, patrolType,startTime, endTime,pageNo,pageSize);
        return RespBodyObj.ok(patrolManageStatistics);
    }

    @PostMapping("getDetail")
    @ApiOperation(value = "获取达标率",notes="")
    public RespBodyObj<List<PatrolManageUserData>> getDetail(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        String userNo = org.springframework.util.StringUtils.isEmpty(params.getData().get("userNo"))?null:params.getData().get("userNo").toString();
        Integer patrolType = org.springframework.util.StringUtils.isEmpty(params.getData().get("patrolType"))?null:Integer.valueOf(params.getData().get("patrolType").toString());
        Long bindNo = org.springframework.util.StringUtils.isEmpty(params.getData().get("bindNo"))?null:Long.valueOf(params.getData().get("bindNo").toString());
        List<PatrolManageUserData> patrolManageStatistics =patrolManageStatisticsService.getDetail(userNo, patrolType,bindNo);
        return RespBodyObj.ok(patrolManageStatistics);
    }

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

    /**
     * patrolManageStatistics添加或编辑
     * @param param
     * @return
     */
    @PostMapping("saveOrupdate")
    @ApiOperation(value = "新增或编辑patrol_manage_statistics",notes="根据实体修改patrol_manage_statistics")
    public RespBodyObj saveOrupdate(@RequestBody ReqBodyObj<PatrolManageStatistics> param) {
        try {
            boolean b = patrolManageStatisticsService.insertOrUpdate(param.getData());
            return b ? RespBodyObj.ok(Constant.SUCCESS) : RespBodyObj.error(Constant.FAILED);
        } catch (Exception e) {
            log.error("/patrolManageStatistics/saveOrupdate", e);
            return RespBodyObj.error(Constant.ERROR);
        }
    }

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

   /**
   * patrolManageStatistics查询单个
   *
   * @param params
   * @return
   */
    @PostMapping("selectOne")
    @ApiOperation(value = "查询patrol_manage_statistics",notes="根据主键查询patrol_manage_statistics")
    public RespBodyObj<PatrolManageStatistics> 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();
        PatrolManageStatistics patrolManageStatistics = patrolManageStatisticsService.selectById(id);
        return RespBodyObj.ok(patrolManageStatistics);
    }


    /**
     * 导出功能
     * @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) {
        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("是否达标");
        try {
            String userName = org.springframework.util.StringUtils.isEmpty(params.getData().get("userName"))?null:params.getData().get("userName").toString();
            Integer patrolType = org.springframework.util.StringUtils.isEmpty(params.getData().get("patrolType"))?null: Integer.valueOf(params.getData().get("patrolType").toString());
            List<PatrolManageUserRelation> list = patrolManageUserRelationMapper.selectByUsername(userName,patrolType);
            List<List> result = new ArrayList<>();
            if(!list.isEmpty()){
                for (PatrolManageUserRelation relation : list) {
                    List<String> str = new ArrayList<>();
                    List<PatrolManageUserData> detail = patrolManageStatisticsService.getDetail(userName, patrolType,relation.getBindNo());
                    if(detail!=null && !detail.isEmpty()){
                        str.add(relation.getHandlerName());
                        str.add(relation.getProjectName());
                        for (PatrolManageUserData data : detail) {
                            str.add(data.getCount().toString());
                            str.add(String.valueOf(data.getPatrolHour()));
                            str.add(data.getState()==1?"达标":"未达标");
                        }
                    }
                    result.add(str);
                }
            }
            ExportExcel excel = new ExportExcel();
            excel.exportExcel3("巡查记录", headers, result, out, response,(new Date().toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}