package com.mosty.yjzl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.model.dto.yjzl.TbZdxlFgdwSaveDTO;
import com.mosty.base.model.dto.yjzl.TbZdxlFgdwUpdateDTO;
import com.mosty.base.model.query.yjzl.TbZdxlFgdwQuery;
import com.mosty.base.model.vo.yjzl.TbZdxlFgdwVO;
import com.mosty.base.utils.MessageUtils;
import com.mosty.base.utils.spring.SpringValidUtils;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.token.JwtSysUser;
import com.mosty.yjzl.service.TbZdxlFgdwService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author zhangzhao
 * @description 指导巡逻方格点位接口
 * @modifier zhangzhao
 * @modifiedTime 2025/05/15 21:53
 * @since 2025/05/15 21:53
 */
@Api(tags = {"指导巡逻-方格点位接口"})
@RestController
@AllArgsConstructor
    @RequestMapping("/tbZdxlFgdw")
public class TbZdxlFgdwController {

    /**
     * 指导巡逻方格点位表Service
     */
    private final TbZdxlFgdwService tbZdxlFgdwService;

    /**
     * 查询单条（根据主键ID）
     * @param id 主键ID
     * @return ResponseResult 实体信息
     */
    @ApiOperation(value = "查询单条（根据主键ID）")
    @GetMapping("{id}")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-查询单条（根据主键ID）", businessType = BusinessType.OTHER)
    public ResponseResult<TbZdxlFgdwVO> selectById(@PathVariable("id") Long id) {
        return ResponseResult.success(tbZdxlFgdwService.selectById(id));
    }

    /**
     * 查询全量列表
     * @return ResponseResult 实体信息列表
     */
    @ApiOperation(value = "查询全量列表")
    @GetMapping("/selectAllList")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-查询全量列表", businessType = BusinessType.OTHER)
    public ResponseResult<List<TbZdxlFgdwVO>> selectAllList() {
        return ResponseResult.success(tbZdxlFgdwService.selectAllList());
    }

    /**
     * 查询分页
     * @param query 实体查询对象
     * @return ResponseResult 实体信息分页列表
     */
    @ApiOperation(value = "查询分页")
    @GetMapping("/selectPage")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-查询分页", businessType = BusinessType.OTHER)
    public ResponseResult<IPage<TbZdxlFgdwVO>> selectPage(TbZdxlFgdwQuery query) {
        return ResponseResult.success(tbZdxlFgdwService.selectPage(query));
    }

    /**
     * 查询列表
     * @param query 实体查询对象
     * @return ResponseResult 实体信息列表
     */
    @ApiOperation(value = "查询列表")
    @GetMapping("/selectList")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-查询列表", businessType = BusinessType.OTHER)
    public ResponseResult<List<TbZdxlFgdwVO>> selectList(TbZdxlFgdwQuery query) {
        return ResponseResult.success(tbZdxlFgdwService.selectList(query));
    }

    /**
     * 新增单条
     * @param dto 保存DTO对象
     * @param bindResult 校验对象
     * @return ResponseResult 实体主键ID
     */
    @ApiOperation(value = "新增单条")
    @PostMapping("/save")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-新增单条", businessType = BusinessType.INSERT)
    public ResponseResult<Long> save(@RequestBody @Valid TbZdxlFgdwSaveDTO dto, BindingResult bindResult){
        try {
            //基础信息校验
            String message = SpringValidUtils.getErrorsMsg(bindResult);
            if (StringUtils.isNotBlank(message)) {
                return ResponseResult.fail(MessageUtils.getSaveFailMsg() + message);
            }

            //新增
            Long resultId = tbZdxlFgdwService.saveEntity(dto);
            if(ObjectUtils.isNotEmpty(resultId)){
                return ResponseResult.success(resultId);
            }
            return ResponseResult.fail(MessageUtils.getSaveFailMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(MessageUtils.getSaveServerErrorMsg());
        }
    }

    /**
     * 修改单条
     * @param dto 修改DTO对象
     * @param bindResult 校验对象
     * @return ResponseResult 实体主键ID
     */
    @ApiOperation(value = "修改单条")
    @PostMapping("update")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-修改单条", businessType = BusinessType.UPDATE)
    public ResponseResult<Long> update(@RequestBody @Valid TbZdxlFgdwUpdateDTO dto, BindingResult bindResult){
        try {
            //基础信息校验
            String message = SpringValidUtils.getErrorsMsg(bindResult);
            if (StringUtils.isNotBlank(message)) {
                return ResponseResult.fail(MessageUtils.getEditFailMsg() + message);
            }

            //修改
            Long resultId = tbZdxlFgdwService.updateEntity(dto);
            if(ObjectUtils.isNotEmpty(resultId)){
                return ResponseResult.success(resultId);
            }
            return ResponseResult.fail(MessageUtils.getEditFailMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(MessageUtils.getEditServerErrorMsg());
        }
    }

    /**
     * 根据经纬度获取正方形四个点位
     * @param longitude 经度
     * @param latitude 纬度
     * @param distance 范围（米），半径
     * @return ResponseResult 正方形四个角点坐标
     */
    @ApiOperation(value = "根据经纬度获取正方形四个点位")
    @GetMapping("/getSquarePoints")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-获取正方形点位", businessType = BusinessType.OTHER)
    public ResponseResult<Map<String, BigDecimal>> getSquarePoints(
            @RequestParam("longitude") Double longitude,
            @RequestParam("latitude") Double latitude,
            @RequestParam(value = "distance", defaultValue = "500") Double distance) {
        return ResponseResult.success(tbZdxlFgdwService.getSquarePoints(longitude, latitude, distance));
    }

    /**
     * 删除单条（逻辑删除）
     * @param id 主键ID
     * @return ResponseResult 删除结果
     */
    @ApiOperation(value = "删除单条")
    @DeleteMapping("/{id}")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-删除单条", businessType = BusinessType.DELETE)
    public ResponseResult<Integer> deleteById(@PathVariable("id") Long id) {
        return ResponseResult.success(tbZdxlFgdwService.deleteById(id));
    }

    /**
     * 批量删除（逻辑删除）
     * @param ids 主键ID列表
     * @return ResponseResult 删除结果
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batch")
    @JwtSysUser
    @Log(title = "指导巡逻-方格点位接口-批量删除", businessType = BusinessType.DELETE)
    public ResponseResult<Integer> deleteByIds(@RequestParam List<Long> ids) {
        return ResponseResult.success(tbZdxlFgdwService.deleteByIds(ids));
    }

}
