package com.yeejoin.amos.maintenance.business.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;

import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
import com.yeejoin.amos.maintenance.business.dto.PointDto;
import com.yeejoin.amos.maintenance.business.param.MovePointParam;
import com.yeejoin.amos.maintenance.business.param.PointImportParam;
import com.yeejoin.amos.maintenance.business.param.PointImportQueryParam;
import com.yeejoin.amos.maintenance.business.param.PointParam;
import com.yeejoin.amos.maintenance.business.service.intfc.ICatalogTreeService;
import com.yeejoin.amos.maintenance.business.service.intfc.IPointService;
import com.yeejoin.amos.maintenance.business.util.CommonResponse;
import com.yeejoin.amos.maintenance.business.util.CommonResponseUtil;
import com.yeejoin.amos.maintenance.business.util.DaoCriteria;
import com.yeejoin.amos.maintenance.business.util.FileHelper;
import com.yeejoin.amos.maintenance.business.util.PointParamUtils;
import com.yeejoin.amos.maintenance.business.vo.MaintenanceResourceData;
import com.yeejoin.amos.maintenance.business.vo.PointClassifyVo;
import com.yeejoin.amos.maintenance.business.vo.PointInputItemVo;
import com.yeejoin.amos.maintenance.business.vo.PointVo;
import com.yeejoin.amos.maintenance.core.common.request.CommonPageable;
import com.yeejoin.amos.maintenance.core.common.request.CommonRequest;
import com.yeejoin.amos.maintenance.core.common.response.PointResponse;
import com.yeejoin.amos.maintenance.dao.entity.Point;
import com.yeejoin.amos.maintenance.dao.entity.PointClassify;
import com.yeejoin.amos.maintenance.feign.RemoteSecurityService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/api/point")
@Api(tags = "巡检点api")
public class PointController extends AbstractBaseController {

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

    @Autowired
    private IPointService iPointService;

    @Autowired
    private ICatalogTreeService catalogTreeService;

    @Autowired
    private RemoteSecurityService remoteSecurityService;

    /**
     * 增加巡检点
     *
     * @param pointParam
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新增巡检点", notes = "新增巡检点")
    @PostMapping(value = "/addPoint", produces = "application/json;charset=UTF-8")
    public CommonResponse addPoint(@ApiParam(value = "巡检点", required = true) @RequestBody PointParam pointParam) {
        AgencyUserModel user = getUserInfo();
        if (ObjectUtils.isEmpty(user)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            ReginParams reginParams = getSelectedOrgInfo();
            String loginOrgCode = getOrgCode(reginParams);
            //点归属于公司
            pointParam.getPoint().setOrgCode(loginOrgCode);
            pointParam.getPoint().setCreatorId(user.getUserId());
            Point point = iPointService.addPoint(pointParam);
            return CommonResponseUtil.success(point);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("巡检点新增失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新增移动巡检点(<font color='blue'>手机app</font>)", notes = "新增移动巡检点(<font color='blue'>手机app</font>)")
    @PostMapping(value = "/addMovePoint", produces = "application/json;charset=UTF-8")
    public CommonResponse addMovePoint(@RequestBody MovePointParam param) {
        AgencyUserModel user = getUserInfo();
        if (ObjectUtils.isEmpty(user)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            ReginParams reginParams = getSelectedOrgInfo();
            String loginOrgCode = getOrgCode(reginParams);
            //点归属于公司
            param.setOrgCode(loginOrgCode);
            param.setCreatorId(user.getUserId());
            iPointService.addMovePoint(param);
            return CommonResponseUtil.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("巡检点新增失败");
        }
    }

    /**
     * 删除巡检点
     *
     * @param ids
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "删除巡检点", notes = "删除巡检点")
    @DeleteMapping(value = "/deletePoint", produces = "application/json;charset=UTF-8")
    public CommonResponse deletePoint(@ApiParam(value = "巡检点ID", required = true) @RequestParam Long[] ids) {
        try {
            iPointService.delPointById(ids);
            return CommonResponseUtil.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("删除巡检点失败");
        }
    }

    /**
     * 更新巡检点
     *
     * @param pointParam
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "更新巡检点", notes = "更新巡检点")
    @PutMapping(value = "/updatePoint", produces = "application/json;charset=UTF-8")
    public CommonResponse updatePoint(@ApiParam(value = "巡检点", required = true) @RequestBody PointParam pointParam) {
        try {
            iPointService.updatePoint(pointParam);
            return CommonResponseUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("更新巡检点失败");
        }
    }

    /**
     * 根据ID查询巡检点信息
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息", notes = "查询巡检点信息")
    @GetMapping(value = "/queryPointById/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointById(@ApiParam(value = "巡检点id", required = true) @PathVariable(name = "id") Long id) {
        try {
            Point point = iPointService.queryPointById(id);
            return CommonResponseUtil.success(point);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点详情<font color='blue'>手机app</font>)", notes = "查询巡检点详情<font color='blue'>手机app</font>)")
    @GetMapping(value = "/queryPointDetailById", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointDetailById(@ApiParam(value = "巡检点id", required = true) @RequestParam(name = "pointId") Long id) {
        try {
            Map point = iPointService.queryPointDetailById1(getToken(), getProduct(), getAppKey(), id);
            return CommonResponseUtil.success(point);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点详情失败");
        }

    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点详情<font color='blue'>手机app</font>)", notes = "查询巡检点详情<font color='blue'>手机app</font>)")
    @GetMapping(value = "/v2/queryPointDetailById", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointDetailByIdInVersion2(@ApiParam(value = "巡检点id", required = true) @RequestParam(name = "pointId") Long id) {
        try {
            Map point = iPointService.queryPointDetailByIdInVersion2(id);
            return CommonResponseUtil.success(point);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点详情失败");
        }

    }

    /**
     * 查询巡检点信息
     *
     * @param point
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息", notes = "查询巡检点信息")
    @PostMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public CommonResponse listPoint(
            @ApiParam(value = "查询条件") @RequestBody(required = false) List<CommonRequest> queryRequests,
            @ApiParam(value = "分页参数", required = true, defaultValue = "pageNumber=0&pageSize=10") CommonPageable commonPageable) {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            CommonRequest commonRequest = new CommonRequest();
            commonRequest.setName("orgCode");
            commonRequest.setValue(loginOrgCode);
            queryRequests.add(commonRequest);
            List<DaoCriteria> criterias = buildDaoCriterias(queryRequests, false, loginOrgCode, null);
            Page<PointVo> pointList = iPointService.queryPointInfo(criterias, commonPageable, loginOrgCode);
            return CommonResponseUtil.success(pointList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点信息失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息", notes = "查询巡检点信息")
    @PostMapping(value = "/{ownerId}/list-with-item", produces = "application/json;charset=UTF-8")
    public CommonResponse listPointWithRoute(
            @ApiParam(value = "查询条件") @RequestBody(required = false) List<CommonRequest> queryRequests,
            @ApiParam(value = "分页参数", required = true, defaultValue = "pageNumber=0&pageSize=10") CommonPageable commonPageable,
            @PathVariable Long ownerId) {
        try {
            CommonRequest commonRequest = new CommonRequest();
            commonRequest.setName("ownerId");
            commonRequest.setValue(ownerId);
            queryRequests.add(commonRequest);
            List<DaoCriteria> criterias = buildDaoCriterias(queryRequests, false, null, null);
            Page<PointDto> pointList = iPointService.queryPointInfoWithItem(criterias, commonPageable, ownerId);
            return CommonResponseUtil.success(pointList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点信息失败");
        }
    }

    /**
     * 查询巡检点信息
     *
     * @param point
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询所有检查点", notes = "查询所有检查点")
    @PostMapping(value = "/list/all", produces = "application/json;charset=UTF-8")
    public CommonResponse listAllPoint() {
        List<CommonRequest> queryRequests = new ArrayList<>();
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setName("orgCode");
        commonRequest.setValue(loginOrgCode);
        queryRequests.add(commonRequest);
        List<DaoCriteria> criterias = buildDaoCriterias(queryRequests, false, loginOrgCode, null);
        return CommonResponseUtil.success(iPointService.queryAllPoint(criterias));
    }

    /**
     * 导入巡检点
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传巡检点", notes = "上传巡检点")
    @RequestMapping(value = "/import", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public CommonResponse importPoint(@ApiParam(value = "导入数据文件", required = true) @RequestBody(required = true) MultipartFile file) {
        String fileName = file.getOriginalFilename();

        if (!fileName.endsWith(".xls")) {
            return CommonResponseUtil.failure("文件格式错误");
        }
        String userId = getUserId();
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            List<PointImportParam> list = FileHelper.importExcel(file, 0, 1, PointImportParam.class);
            iPointService.addImportPoint(list, loginOrgCode, userId);
            return CommonResponseUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponseUtil.failure("导入失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "导入导入模板", notes = "导入导入模板")
    @PostMapping(value = "/exportTemplate", produces = "application/json;charset=UTF-8")
    public CommonResponse exportTemplate(HttpServletResponse response) {
        String userId = getUserId();
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        String fileName = "巡检点导入模板"; //模板名称
        String[] handers = {"点编号", "点名称", "是否为移动点", "计划外巡检是否评分", "是否允许离线巡检", "参考地址", "3维地图坐标", "3维地图楼层", "最少拍照数量", "最多拍照数量", "风险等级", "备注说明", "所属分类目录", "检查项目", "是否室内"}; //列标题
        List<String[]> downData = new ArrayList();
        String[] isScore = {"否", "是"};
        String[] isFixed = {"否", "是"};
        String[] offline = {"否", "是"};
        String[] isIndoor = {"否", "是"};
        String[] level = {"一级", "二级", "三级", "四级", "五级"};
        String[] catalog = catalogTreeService.getCataLogList(loginOrgCode);
        downData.add(isFixed);
        downData.add(isScore);
        downData.add(offline);
        downData.add(level);
        downData.add(catalog);
        downData.add(isIndoor);
        String[] downRows = {"2", "3", "4", "10", "12", "14"}; //下拉的列序号数组(序号从0开始)

        try {
            FileHelper.createExcelTemplate(fileName, handers, downData, downRows, response);
        } catch (Exception e) {
            CommonResponseUtil.failure(e.getMessage());
        }
        return CommonResponseUtil.success();
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息", notes = "查询巡检点信息")
    @PostMapping(value = "/exportListPoint", produces = "application/json;charset=UTF-8")
    public CommonResponse exportListPoint(@ApiParam(value = "查询条件", required = false) @RequestBody(required = false) List<CommonRequest> queryRequests,
                                          @RequestParam(value = "exportType", required = false) int exportType,
                                          HttpServletResponse response) {
        ReginParams reginParams = getSelectedOrgInfo();
        String orgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            PointImportQueryParam pointImportQueryParam = PointParamUtils.fillPointImportQuery(queryRequests);
            pointImportQueryParam.setOrgCode(orgCode);
            List<PointVo> pointList = iPointService.queryPointInfoListByIds(pointImportQueryParam);
            if (exportType == 0) {
                FileHelper.exportExcel(pointList, "巡检点列表", "巡检点信息", PointVo.class, "巡检点.xls", response);
            } else if (exportType == 1) {
                FileHelper.exportQRCode(pointList, "巡检点二维码", response);
            }
            return CommonResponseUtil.success("导出成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点信息失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息", notes = "查询巡检点信息")
    @GetMapping(value = "/exportListPointByIds", produces = "application/json;charset=UTF-8")
    public CommonResponse exportListPointByIds(@ApiParam(value = "查询条件", required = false) @RequestParam(value = "ids", required = false) Long[] ids,
                                               @RequestParam(value = "exportType", required = false) int exportType,
                                               HttpServletResponse response) {
        ReginParams reginParams = getSelectedOrgInfo();
        String orgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            List<Long> idList = Arrays.asList(ids);
            PointImportQueryParam pointImportQueryParam = new PointImportQueryParam();
            pointImportQueryParam.setPointIds(idList);
            pointImportQueryParam.setOrgCode(orgCode);
            List<PointVo> pointList = iPointService.queryPointInfoListByIds(pointImportQueryParam);
            if (exportType == 0) {
                FileHelper.exportExcel(pointList, "巡检点列表", "巡检点信息", PointVo.class, "巡检点.xls", response);
            } else if (exportType == 1) {
                FileHelper.exportQRCode(pointList, "巡检点二维码", response);
            }
            return CommonResponseUtil.success("导出成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点信息失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点信息(<font color='blue'>手机app</font>)", notes = "查询巡检点信息(<font color='blue'>手机app</font>)")
    @PostMapping(value = "/queryPointByPage", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointByPage(
            @ApiParam(value = "查询条件", required = false) @RequestBody(required = false) List<CommonRequest> queryRequests,
            @ApiParam(value = "分页参数", required = true, defaultValue = "pageNumber=0&pageSize=10") CommonPageable commonPageable) {

        ReginParams reginParams = getSelectedOrgInfo();
        String orgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            CommonRequest commonRequest = new CommonRequest();
            commonRequest.setName("orgCode");
            commonRequest.setValue(orgCode);
            queryRequests.add(commonRequest);
            HashMap<String, Object> param = PointParamUtils.fillTaskInfo(queryRequests);
            Page<PointVo> pointList = iPointService.queryPointByPage(param, commonPageable);
            return CommonResponseUtil.success(pointList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点信息失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询用户登录公司全部巡检点", notes = "查询用户登录公司全部巡检点")
    @GetMapping(value = "/authPoints", produces = "application/json;charset=UTF-8")
    public CommonResponse listPoint(@ApiParam(value = "巡检点编码", required = false) @RequestParam("ids") List<String> ids,
                                    @ApiParam(value = "巡检点名称", required = false) @RequestParam("names") List<String> names) {
        ReginParams reginParams = getSelectedOrgInfo();
        String orgCode = getOrgCode(reginParams);
        try {
            List<Point> list = iPointService.queryPointList(ids, names, orgCode);
            return CommonResponseUtil.success(list);
        } catch (Exception e) {
            return CommonResponseUtil.failure(e.getMessage());
        }
    }

    /**
     * 查询巡检点信息及所包含得分类
     *
     * @param code
     * @param name
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询用户登录公司全部巡检点", notes = "查询用户登录公司全部巡检点")
    @GetMapping(value = "/queryPoints", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPoints(@ApiParam(value = "巡检点编码", required = false) @RequestParam("code") String code,
                                      @ApiParam(value = "巡检点名称", required = false) @RequestParam("name") String name) {
        ReginParams reginParams = getSelectedOrgInfo();
        String orgCode = getOrgCode(reginParams);
        try {
            List<HashMap<String, Object>> list = iPointService.findPointList(code, name, orgCode);
            return CommonResponseUtil.success(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResponseUtil.failure(e.getMessage());
        }
    }

    /**
     * 另存巡检点
     *
     * @param ids
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "另存巡检点", notes = "另存巡检点")
    @PostMapping(value = "/saveAs", produces = "application/json;charset=UTF-8")
    public CommonResponse saveAs(@ApiParam(value = "巡检点id集合", required = true) @RequestParam Long[] ids) {
        try {
            List<String> list = iPointService.qryPointNoList(null, (long) -1);
            for (Long id : ids) {
                Point point = iPointService.queryPointById(id);
                if (list.contains(point.getPointNo() + "_copy")) {
                    return CommonResponseUtil.failure("巡检点" + point.getPointNo() + "已另存!");
                }
            }
            iPointService.saveAs(ids);
            return CommonResponseUtil.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("另存巡检点失败");
        }
    }

    /**
     * 查询巡检点的巡检项
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点的巡检项", notes = "查询巡检点的巡检项")
    @PostMapping(value = "/queryPointInputItem", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointInputItem(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId) {
        try {
            List<PointInputItemVo> pointInputItemList = iPointService.queryPointInputItem(pointId);
            return CommonResponseUtil.success(pointInputItemList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点的巡检项失败");
        }
    }

    /**
     * 查询巡检点的巡检项
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点的巡检项", notes = "查询巡检点的巡检项")
    @GetMapping(value = "/queryEquipPointInputItem", produces = "application/json;charset=UTF-8")
    public CommonResponse queryEquipPointInputItem(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId) {
        try {
            List<Map<String, Object>> pointInputItemList = iPointService.queryEquipPointInputItem(pointId);
            return CommonResponseUtil.success(pointInputItemList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点的巡检项失败");
        }
    }

    /**
     * 查询巡检点的分类
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点的分类", notes = "查询巡检点的分类")
    @PostMapping(value = "/queryPointClassify", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointClassify(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId) {
        try {
            List<PointClassify> pointClassify = iPointService.queryPointClassify(pointId);
            return CommonResponseUtil.success(pointClassify);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点的分类失败");
        }
    }

    /**
     * 根据分类id查询对应项
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "根据分类id查询对应项", notes = "根据分类id查询对应项")
    @PostMapping(value = "/queryPointInputItemByClassify", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointInputItemByClassify(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId,
                                                        @ApiParam(value = "巡检点分类id", required = true) @RequestParam Long classifyId) {
        try {
            List<PointInputItemVo> pointInputItem = iPointService.queryPointInputItemByClassify(pointId, classifyId);
            return CommonResponseUtil.success(pointInputItem);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("根据分类id查询对应项失败");
        }
    }

    /**
     * 根据点id查询点的分类及对应项
     *
     * @param id
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "根据点id查询点的分类及对应项", notes = "根据点id查询点的分类及对应项")
    @PostMapping(value = "/queryPointClassifyWithInputItem", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointClassifyWithInputItem(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId) {
        try {
            Map<String, PointClassifyVo> classifyNameWithInputItemIdMap = iPointService.queryPointClassifyWithInputItemVo(pointId);
            return CommonResponseUtil.success(classifyNameWithInputItemIdMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("根据点id查询点的分类及对应项失败");
        }
    }

    /**
     * 设置巡检点巡检模式
     *
     * @param pointId
     * @param isOffine
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "设置巡检点巡检模式", notes = "设置巡检点巡检模式")
    @PostMapping(value = "/setPatrolMode", produces = "application/json;charset=UTF-8")
    public CommonResponse setPatrolMode(@ApiParam(value = "巡检点id", required = true) @RequestParam Long pointId,
                                        @ApiParam(value = "是否开启离线模式", required = true) @RequestParam Boolean isOffine) {
        try {
            Point point = iPointService.setPatrolMode(pointId, isOffine);
            if (ObjectUtils.isEmpty(point)) {
                return CommonResponseUtil.failure("该巡检点不存在");
            }
            return CommonResponseUtil.success(point);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("绑卡失败");
        }
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询用户权限点信息饼图(<font color='blue'>手机app</font>)", notes = "查询用户权限点信息饼图(<font color='blue'>手机app</font>)")
    @GetMapping(value = "/queryPointPie", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointPie() {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orgCode", loginOrgCode);
        HashMap<String, Object> response = iPointService.queryPointPie(params);
        return CommonResponseUtil.success(response);
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询用户权限点信息柱状图(<font color='blue'>手机app</font>)", notes = "查查询用户权限点信息柱状图(<font color='blue'>手机app</font>)")
    @GetMapping(value = "/queryPointHistogram", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointHistogram() {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orgCode", loginOrgCode);
        List<HashMap<String, Object>> response = iPointService.queryPointHistogram(params);
        //安全处里
        if (response != null && response.size() > 0) {
            //获取部门名称
            String userIds = "";
            Set<String> set = new HashSet<>();
            response.forEach(s -> {
                if (!"其他".equals(s.get("departmentName") + "")) {
                    set.add(s.get("departmentName").toString());
                }
            });

            List<String> list1 = new ArrayList<>(set);
            userIds = String.join(",", list1);
            List<LinkedHashMap> listd = remoteSecurityService.listDepartmentByDeptIds(getToken(), getProduct(), getAppKey(), userIds);
            Map<String, String> deptMap = new HashMap<String, String>();
            if (listd != null && listd.size() > 0) {
                for (int i = 0; i < listd.size(); i++) {
                    deptMap.put(listd.get(i).get("sequenceNbr").toString(), listd.get(i).get("departmentName").toString());
                }
            }
            //新安全
            response.forEach(s -> {
                if (!"其他".equals(s.get("departmentName") + "")) {
                    s.put("departmentName", deptMap.get(s.get("departmentName") + ""));
                }
            });
        }


        return CommonResponseUtil.success(response);
    }

    /**
     * 查询巡检点信息
     *
     * @param point
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询巡检点编号信息", notes = "查询巡检点编号信息")
    @GetMapping(value = "/queryPointNo/{pointId}", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointNoList(
            @ApiParam(value = "巡检点id", required = true) @PathVariable(name = "pointId") Long pointId,
            @ApiParam(value = "查询条件", required = false) @RequestBody(required = false) List<CommonRequest> queryRequests) {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        try {
            CommonRequest commonRequest = new CommonRequest();
            commonRequest.setName("orgCode");
            commonRequest.setValue(loginOrgCode);
            if (queryRequests == null) {
                queryRequests = new ArrayList<>();
            }
            queryRequests.add(commonRequest);
            List<DaoCriteria> criterias = buildDaoCriterias(queryRequests, false, loginOrgCode, null);
            List<String> pointNoList = iPointService.qryPointNoList(criterias, pointId);
            return CommonResponseUtil.success(pointNoList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResponseUtil.failure("查询巡检点编号信息失败");
        }
    }

    /**
     * 离线点信息同步
     *
     * @param startTime
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询离线点信息", notes = "查询离线点信息")
    @GetMapping(value = "/queryLeavelPoint", produces = "application/json;charset=UTF-8")
    public CommonResponse queryLeavelPoint(@ApiParam(value = "数据同步开始时间（不传为全部同步）", required = false) @RequestParam(required = false) String startTime) {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        if (ObjectUtils.isEmpty(reginParams)) {
            return CommonResponseUtil.failure("用户session过期");
        }
        return CommonResponseUtil.success(iPointService.queryLeavelPoint(getToken(), getProduct(), getAppKey(), loginOrgCode, startTime));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询点信息json", notes = "查询点信息json")
    @GetMapping(value = "/getPointConfigJson", produces = "application/json;charset=UTF-8")
    public CommonResponse getPointConfigJson() {

        return CommonResponseUtil.success(iPointService.getPointConfigDom());
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "查询点信息xml", notes = "查询点信息xml")
    @GetMapping(value = "/getPointConfigDom", produces = MediaType.APPLICATION_XML_VALUE)
    public List<PointResponse> getPointConfigDom() {

        return iPointService.getPointConfigDom();
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "导出IEC104点位表", notes = "导出IEC104点位表")
    @PostMapping(value = "/exportPoint/iec", produces = "application/json;charset=UTF-8")
    public CommonResponse exportIecPoint(HttpServletResponse response) {
        ReginParams reginParams = getSelectedOrgInfo();
        String loginOrgCode = getOrgCode(reginParams);
        String fileName = "巡检点点位"; //模板名称
        List<Map<String, Object>> list = iPointService.queryAllPointConfig(loginOrgCode);
        try {
            FileHelper.exportExcel(list, fileName, response);
        } catch (Exception e) {
            CommonResponseUtil.failure(e.getMessage());
        }
        return CommonResponseUtil.success();
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新*巡检点筛选分页列表", notes = "新*巡检点筛选分页列表")
    @GetMapping(value = "/list-new", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointList4Route(
            @ApiParam(value = "区域") @RequestParam(value = "riskSourceId", required = false) Long riskSourceId,
            @ApiParam(value = "点编号") @RequestParam(value = "pointNo", required = false) String pointNo,
            @ApiParam(value = "点名称") @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        return CommonResponseUtil.success(iPointService.queryPointList4Route(riskSourceId, pointNo, name, current, size));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新*查询巡检点列表", notes = "新*查询巡检点列表")
    @GetMapping(value = "/route-list-new", produces = "application/json;charset=UTF-8")
    public CommonResponse queryPointListByRouteId(
            @ApiParam(value = "线路ID") @RequestParam(value = "routeId") Long routeId) {
        return CommonResponseUtil.success(iPointService.queryPointListByRouteId(routeId));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新*查询巡检点对应设备列表", notes = "新*查询巡检点对应设备列表")
    @GetMapping(value = "/equip-list-new", produces = "application/json;charset=UTF-8")
    public CommonResponse queryEquipListByPointId(
            @ApiParam(value = "巡检点ID") @RequestParam(value = "pointId") Long pointId) {
        return CommonResponseUtil.success(iPointService.queryEquipListByPointId(pointId));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新*查询巡检点对应设备列表", notes = "新*查询巡检点对应设备列表")
    @GetMapping(value = "/item-list-new", produces = "application/json;charset=UTF-8")
    public CommonResponse queryItemList4RoutePoint(
            @ApiParam(value = "巡检点ID") @RequestParam(value = "pointId") Long pointId,
            @ApiParam(value = "设备ID") @RequestParam(value = "classifyId") Long classifyId) {
        return CommonResponseUtil.success(iPointService.queryItemList4RoutePoint(pointId, classifyId));
    }

    /**
     * 获取所属区域树
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "新*查询巡检点列表", notes = "新*查询巡检点列表")
    @GetMapping(value = "/regionTress", produces = "application/json;charset=UTF-8")
    public CommonResponse getRegionTress() {
        return CommonResponseUtil.success(iPointService.getRegionTress());
    }

    /**
     * 同步点
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "装备维保项", notes = "装备维保项")
    @RequestMapping(value = "/equipmentInput", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public CommonResponse syncSavePoint(
            @ApiParam(value = "同步请求参数") @RequestBody() List<MaintenanceResourceData> list) {
        ReginParams reginParams = getSelectedOrgInfo();
        AgencyUserModel model = getUserInfo();
        String orgCode = getOrgCode(reginParams);
        iPointService.syncSavePoint(list, orgCode, model.getUserId());
        return CommonResponseUtil.success();
    }

}
