package com.dy.pipIrrBase.block; 
 | 
  
 | 
import com.alibaba.fastjson2.JSONArray; 
 | 
import com.alibaba.fastjson2.JSONObject; 
 | 
import com.dy.common.aop.SsoAop; 
 | 
import com.dy.common.mybatis.envm.Deleted; 
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.common.webUtil.BaseResponseUtils; 
 | 
import com.dy.common.webUtil.QueryResultVo; 
 | 
import com.dy.common.webUtil.ResultCodeMsg; 
 | 
import com.dy.pipIrrBase.block.dto.Block; 
 | 
import com.dy.pipIrrBase.block.enums.graphTypeENUM; 
 | 
import com.dy.pipIrrBase.result.SystemResultCode; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaBlock; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaMapCoordinates; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaMapGraph; 
 | 
import com.dy.pipIrrGlobal.voBa.VoBlock; 
 | 
import io.swagger.v3.oas.annotations.Operation; 
 | 
import io.swagger.v3.oas.annotations.media.Content; 
 | 
import io.swagger.v3.oas.annotations.media.Schema; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponse; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponses; 
 | 
import io.swagger.v3.oas.annotations.tags.Tag; 
 | 
import jakarta.validation.Valid; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.validation.BindingResult; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.util.List; 
 | 
import java.util.Objects; 
 | 
import java.util.Optional; 
 | 
  
 | 
@Slf4j 
 | 
@Tag(name = "片区管理", description = "片区增删改查等操作") 
 | 
@RestController 
 | 
@RequestMapping(path = "block") 
 | 
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") 
 | 
public class BlockCtrl { 
 | 
  
 | 
    private BlockSv sv; 
 | 
  
 | 
    @Autowired 
 | 
    private void setSv(BlockSv sv) { 
 | 
        this.sv = sv; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 客户端请求得到所有片区数据 
 | 
     * 
 | 
     * @return 所有片区数据 
 | 
     */ 
 | 
    @Operation(summary = "获得全部片区", description = "返回全部片区数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回全部片区数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = BaBlock.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "all") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<BaBlock>>> all() { 
 | 
        try { 
 | 
            QueryResultVo<List<BaBlock>> res = this.sv.selectAll(); 
 | 
            return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询片区异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 客户端请求得到所有片区数据 2024-08-14废弃 
 | 
     * 
 | 
     * @return 所有片区数据 
 | 
     */ 
 | 
    @PostMapping(path = "some2", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<BaBlock>>> some(@RequestBody QueryVo vo) { 
 | 
        try { 
 | 
            QueryResultVo<List<BaBlock>> res = this.sv.selectSome(vo); 
 | 
            return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询片区异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据指定条件获取片区,2024-08-14新增,替换原来的片区分页查询 
 | 
     * 
 | 
     * @param qo 
 | 
     * @param 
 | 
     * @return 
 | 
     */ 
 | 
    @GetMapping(path = "/some") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<VoBlock>>> getBlocks(QueryVo qo) { 
 | 
        try { 
 | 
            return BaseResponseUtils.buildSuccess(this.sv.getBlocks(qo)); 
 | 
        } catch (Exception e) { 
 | 
            log.error("获取开卡记录异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 得到一个片区数据 
 | 
     * 
 | 
     * @return 一个片区数据 
 | 
     */ 
 | 
    @Operation(summary = "一个片区", description = "得到一个片区数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一个片区数据(BaseResponse.content:{})", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = BaBlock.class))} 
 | 
            ) 
 | 
    }) 
 | 
    //@GetMapping(path = "one", consumes = MediaType.TEXT_PLAIN_VALUE) 
 | 
    @GetMapping(path = "one") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<BaBlock> one(Long id) { 
 | 
        return BaseResponseUtils.buildSuccess(this.sv.selectById(id)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 保存片区 
 | 
     * 
 | 
     * @param po 保存片区form表单对象 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "保存片区", description = "提交片区数据(form表单),进行保存") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @PostMapping(path = "save", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    @SsoAop() 
 | 
    //public BaseResponse<Boolean> save(@RequestBody @Valid BaBlock po, BindingResult bindingResult){ 
 | 
    public BaseResponse<Boolean> save(@RequestBody @Valid Block po, BindingResult bindingResult) { 
 | 
        if (bindingResult != null && bindingResult.hasErrors()) { 
 | 
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); 
 | 
        } 
 | 
  
 | 
        // 添加片区记录 
 | 
        BaBlock baBlock = new BaBlock(); 
 | 
        baBlock.setName(po.getName()); 
 | 
        baBlock.setHeader(po.getHeader()); 
 | 
        baBlock.setPhone(po.getPhone()); 
 | 
        baBlock.setArea(po.getArea()); 
 | 
        baBlock.setColor(po.getColor()); 
 | 
        baBlock.setRemark(po.getRemark()); 
 | 
        baBlock.deleted = Deleted.NO; 
 | 
        Long blockId = Optional.ofNullable(this.sv.save(baBlock)).orElse(0L); 
 | 
        if (blockId.equals(0)) { 
 | 
            return BaseResponseUtils.buildErrorMsg(SystemResultCode.SAVA_BLOCK_ERROR.getMessage()); 
 | 
        } 
 | 
  
 | 
        JSONArray graphs = po.getGraphs(); 
 | 
        if(graphs != null && graphs.size() > 0){ 
 | 
            for (int i = 0; i < graphs.size(); i++) { 
 | 
                JSONObject graph = graphs.getJSONObject(i); 
 | 
  
 | 
                // 添加地图图形记录 
 | 
                BaMapGraph baMapGraph = new BaMapGraph(); 
 | 
                baMapGraph.setBlockId(blockId); 
 | 
                switch (graph.getString("type")) { 
 | 
                    case "CircleMarker": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.CIRCLE_MARKER.getCode()); 
 | 
                        break; 
 | 
                    case "Polygon": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.POLYGON.getCode()); 
 | 
                        break; 
 | 
                    case "Polyline": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.POLYLINE.getCode()); 
 | 
                        break; 
 | 
                } 
 | 
                Long graphId = Optional.ofNullable(this.sv.addMapGraph(baMapGraph)).orElse(0L); 
 | 
                if (graphId.equals(0)) { 
 | 
                    return BaseResponseUtils.buildErrorMsg(SystemResultCode.SAVA_BLOCK_ERROR.getMessage()); 
 | 
                } 
 | 
  
 | 
                // 添加地图图形坐标记录 
 | 
                JSONArray coordinates = graph.getJSONArray("coordinates"); 
 | 
                for (int j = 0; j < coordinates.size(); j++) { 
 | 
                    JSONArray coordinate = coordinates.getJSONArray(j); 
 | 
                    for (int k = 0; k < coordinate.size(); k++) { 
 | 
                        JSONObject point = coordinate.getJSONObject(k); 
 | 
  
 | 
                        BaMapCoordinates mapCoordinates = new BaMapCoordinates(); 
 | 
                        mapCoordinates.setGraphId(graphId); 
 | 
                        mapCoordinates.setLat(point.getBigDecimal("lat")); 
 | 
                        mapCoordinates.setLng(point.getBigDecimal("lng")); 
 | 
                        Long coordinateId = Optional.ofNullable(this.sv.addMapCoordinate(mapCoordinates)).orElse(0L); 
 | 
                        if (coordinateId.equals(0)) { 
 | 
                            return BaseResponseUtils.buildErrorMsg(SystemResultCode.SAVA_BLOCK_ERROR.getMessage()); 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return BaseResponseUtils.buildSuccess(true); 
 | 
  
 | 
        //po.id = null ; 
 | 
        //po.deleted = Deleted.NO; 
 | 
        //int count; 
 | 
        //try { 
 | 
        //    count = this.sv.save(po); 
 | 
        //} catch (Exception e) { 
 | 
        //    log.error("保存片区异常", e); 
 | 
        //    return BaseResponseUtils.buildException(e.getMessage()) ; 
 | 
        //} 
 | 
        //if(count <= 0){ 
 | 
        //    return BaseResponseUtils.buildFail("数据库存储失败") ; 
 | 
        //}else{ 
 | 
        //    return BaseResponseUtils.buildSuccess(true) ; 
 | 
        //} 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 编辑修改片区 2024-08-15 作废 
 | 
     * 
 | 
     * @param po 保存片区form表单对象 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @PostMapping(path = "update2", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> update2(@RequestBody @Valid BaBlock po, BindingResult bindingResult) { 
 | 
        if (bindingResult != null && bindingResult.hasErrors()) { 
 | 
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); 
 | 
        } 
 | 
        if (po.id == null) { 
 | 
            return BaseResponseUtils.buildFail("无数据实体ID"); 
 | 
        } 
 | 
        int count; 
 | 
        try { 
 | 
            count = this.sv.update(po); 
 | 
        } catch (Exception e) { 
 | 
            log.error("保存片区异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
        if (count <= 0) { 
 | 
            return BaseResponseUtils.buildFail("数据库存储失败"); 
 | 
        } else { 
 | 
            return BaseResponseUtils.buildSuccess(true); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 修改片区信息,2024-08-15新增,替换原来的片区修改接口 
 | 
     * 
 | 
     * @param po 
 | 
     * @param bindingResult 
 | 
     * @return 
 | 
     */ 
 | 
    @PostMapping(path = "update", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> update(@RequestBody @Valid Block po, BindingResult bindingResult) { 
 | 
        if (bindingResult != null && bindingResult.hasErrors()) { 
 | 
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); 
 | 
        } 
 | 
  
 | 
        Long blockId = po.getId(); 
 | 
        if (blockId == null) { 
 | 
            return BaseResponseUtils.buildErrorMsg(SystemResultCode.THE_BLOCK_NOT_EXIST.getMessage()); 
 | 
        } 
 | 
  
 | 
        // 修改片区信息 
 | 
        BaBlock baBlock = new BaBlock(); 
 | 
        baBlock.setId(po.getId()); 
 | 
        baBlock.setName(po.getName()); 
 | 
        baBlock.setHeader(po.getHeader()); 
 | 
        baBlock.setPhone(po.getPhone()); 
 | 
        baBlock.setArea(po.getArea()); 
 | 
        baBlock.setColor(po.getColor()); 
 | 
        baBlock.setRemark(po.getRemark()); 
 | 
        baBlock.setDeleted(po.getDeleted()); 
 | 
        int count; 
 | 
        try { 
 | 
            count = this.sv.update(baBlock); 
 | 
        } catch (Exception e) { 
 | 
            log.error("保存片区异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
        if (count <= 0) { 
 | 
            return BaseResponseUtils.buildFail("数据库存储失败"); 
 | 
        } 
 | 
  
 | 
        // 删除地图图形坐标 
 | 
        this.sv.deleteMapCoordinates(blockId); 
 | 
        // 删除片区关联的地图图形 
 | 
        this.sv.deleteMapGraph(blockId); 
 | 
  
 | 
        JSONArray graphs = po.getGraphs(); 
 | 
        if(graphs != null && graphs.size() > 0){ 
 | 
            for (int i = 0; i < graphs.size(); i++) { 
 | 
                JSONObject graph = graphs.getJSONObject(i); 
 | 
  
 | 
                // 添加地图图形记录 
 | 
                BaMapGraph baMapGraph = new BaMapGraph(); 
 | 
                baMapGraph.setBlockId(blockId); 
 | 
                switch (graph.getString("type")) { 
 | 
                    case "CircleMarker": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.CIRCLE_MARKER.getCode()); 
 | 
                        break; 
 | 
                    case "Polygon": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.POLYGON.getCode()); 
 | 
                        break; 
 | 
                    case "Polyline": 
 | 
                        baMapGraph.setGraphType(graphTypeENUM.POLYLINE.getCode()); 
 | 
                        break; 
 | 
                } 
 | 
                Long graphId = Optional.ofNullable(this.sv.addMapGraph(baMapGraph)).orElse(0L); 
 | 
                if (graphId.equals(0)) { 
 | 
                    return BaseResponseUtils.buildErrorMsg(SystemResultCode.SAVA_BLOCK_ERROR.getMessage()); 
 | 
                } 
 | 
  
 | 
                // 添加地图图形坐标记录 
 | 
                JSONArray coordinates = graph.getJSONArray("coordinates"); 
 | 
                for (int j = 0; j < coordinates.size(); j++) { 
 | 
                    JSONArray coordinate = coordinates.getJSONArray(j); 
 | 
                    for (int k = 0; k < coordinate.size(); k++) { 
 | 
                        JSONObject point = coordinate.getJSONObject(k); 
 | 
  
 | 
                        BaMapCoordinates mapCoordinates = new BaMapCoordinates(); 
 | 
                        mapCoordinates.setGraphId(graphId); 
 | 
                        mapCoordinates.setLat(point.getBigDecimal("lat")); 
 | 
                        mapCoordinates.setLng(point.getBigDecimal("lng")); 
 | 
                        Long coordinateId = Optional.ofNullable(this.sv.addMapCoordinate(mapCoordinates)).orElse(0L); 
 | 
                        if (coordinateId.equals(0)) { 
 | 
                            return BaseResponseUtils.buildErrorMsg(SystemResultCode.SAVA_BLOCK_ERROR.getMessage()); 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return BaseResponseUtils.buildSuccess(true); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 删除片区 
 | 
     * 
 | 
     * @param id 片区ID 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "删除片区", description = "提交片区ID,进行逻辑删除") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
  
 | 
    //@GetMapping(path = "delete", consumes = MediaType.TEXT_PLAIN_VALUE) 
 | 
    @GetMapping(path = "delete") 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> delete(Long id) { 
 | 
        if (id == null) { 
 | 
            return BaseResponseUtils.buildFail("id不能为空"); 
 | 
        } 
 | 
        int count; 
 | 
        try { 
 | 
            count = this.sv.delete(id); 
 | 
        } catch (Exception e) { 
 | 
            log.error("保存片区异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
        if (count <= 0) { 
 | 
            return BaseResponseUtils.buildFail("数据库存储失败"); 
 | 
        } 
 | 
  
 | 
        // 删除地图图形坐标 
 | 
        this.sv.deleteMapCoordinates(id); 
 | 
        // 删除片区关联的地图图形 
 | 
        this.sv.deleteMapGraph(id); 
 | 
  
 | 
        return BaseResponseUtils.buildSuccess(true); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获取片区、分水房联动视图,添加农户时使用 
 | 
     * @return 
 | 
     */ 
 | 
    @GetMapping(path = "/block_divides") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<JSONArray> getBlockDivides() { 
 | 
        try { 
 | 
            return BaseResponseUtils.buildSuccess(this.sv.getBlockDivides()); 
 | 
        } catch (Exception e) { 
 | 
            log.error("获取开卡记录异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
} 
 |