package com.ruoyi.web.controller.chenyanlogin.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.chenyanlogin.entity.MapLocation;
import com.ruoyi.web.controller.chenyanlogin.service.IMapLocationService;
import com.ruoyi.web.domain.dto.villageSpecial.VillageSpecialAddRequest;
import com.ruoyi.web.domain.dto.villageSpecial.VillageSpecialEditRequest;
import com.ruoyi.web.domain.dto.villageSpecial.VillageSpecialQueryRequest;
import com.ruoyi.web.domain.entity.ProductionData;
import com.ruoyi.web.domain.vo.VillageSpecial.BaseVillageSpecialVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-10-11
 */
@RestController
@RequestMapping("/map-location")
public class MapLocationController {
    @Autowired
    private IMapLocationService mapLocationService;

    @ApiOperation("添加")
    @PostMapping("/add")
    public AjaxResult addVillageSpecial(@RequestBody MapLocation mapLocation) {
        return success( mapLocationService.save(mapLocation));
    }

    /**
     * 删除特色产业
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    @Transactional
    public AjaxResult deleteVillageSpecial(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        for (String s :ids.split(",")) {
            mapLocationService.removeById(s);
        }
        return error("删除失败");
    }



    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult editVillageSpecial(@RequestBody MapLocation mapLocation) {
        if (mapLocation == null) {
            return error("请求参数为空");
        }
        mapLocationService.updateById(mapLocation);
        return AjaxResult.success();
    }



    @ApiOperation("根据id获取")
    @GetMapping("/get")
    public AjaxResult getVillageSpecialById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }

        return success(mapLocationService.getById(id));
    }

    @ApiOperation("分页获取")
    @PostMapping("/list/page")
    public AjaxResult listVillageSpecialByPage(@RequestBody VillageSpecialQueryRequest villageSpecialQueryRequest) {
        if (villageSpecialQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<MapLocation> page = mapLocationService.page(new Page<MapLocation>(villageSpecialQueryRequest.getPageNum(), villageSpecialQueryRequest.getPageSize())
        ,new QueryWrapper<MapLocation>()
                        .eq(ObjectUtil.isNotNull(villageSpecialQueryRequest.getType()),"type",villageSpecialQueryRequest.getType())
                        .like(StringUtils.isNotEmpty(villageSpecialQueryRequest.getName()),"name",villageSpecialQueryRequest.getName()));
        return success(page);
    }


    @ApiOperation("获取全部")
    @GetMapping("/list/list")
    public AjaxResult getVillageSpecialProductList(@RequestBody VillageSpecialQueryRequest villageSpecialQueryRequest) {
        return success( mapLocationService.list(new QueryWrapper<MapLocation>()
                .eq(ObjectUtil.isNotNull(villageSpecialQueryRequest.getType()),"type",villageSpecialQueryRequest.getType())
                .like(StringUtils.isNotEmpty(villageSpecialQueryRequest.getName()),"name",villageSpecialQueryRequest.getName())));
    }

}
