package com.weather.modules.weather.station.controller;

import cn.hutool.core.bean.BeanUtil;
import com.weather.common.annotation.LogOperation;
import com.weather.common.constant.Constant;
import com.weather.common.page.PageData;
import com.weather.common.utils.ExcelUtils;
import com.weather.common.utils.Result;
import com.weather.common.validator.AssertUtils;
import com.weather.common.validator.ValidatorUtils;
import com.weather.common.validator.group.AddGroup;
import com.weather.common.validator.group.DefaultGroup;
import com.weather.common.validator.group.UpdateGroup;
import com.weather.modules.weather.station.dto.WeatherStationDTO;
import com.weather.modules.weather.station.dto.WeatherStationInfoSimpleDto;
import com.weather.modules.weather.station.entity.WeatherStationEntity;
import com.weather.modules.weather.station.excel.WeatherStationExcel;
import com.weather.modules.weather.station.service.WeatherStationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;

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


/**
 * 气象站点表
 *
 * @author Mark 123
 * @since 1.0.0 2026-03-05
 */
@RestController
@RequestMapping("station/weatherstation")
@Tag(name = "气象站点")
public class WeatherStationController {

    @Resource
    private WeatherStationService weatherStationService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "stationName", description = "区站名称", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "stationCode", description = "区站编号", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("station:weatherstation:page")
    public Result<PageData<WeatherStationDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<WeatherStationDTO> page = weatherStationService.page(params);
        return new Result<PageData<WeatherStationDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("station:weatherstation:info")
    public Result<WeatherStationDTO> get(@PathVariable("id") Long id) {
        WeatherStationEntity weatherStationEntity = weatherStationService.selectById(id);
        WeatherStationDTO data = BeanUtil.copyProperties(weatherStationEntity, WeatherStationDTO.class);
        return new Result<WeatherStationDTO>().ok(data);
    }

    @GetMapping("list")
    @Operation(summary = "区站列表")
    @RequiresPermissions("station:weatherstation:list")
    public Result<List<WeatherStationInfoSimpleDto>> getList() {
        List<WeatherStationInfoSimpleDto> stations = weatherStationService.getStationByDeptId();
        return new Result<List<WeatherStationInfoSimpleDto>>().ok(stations);
    }


    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("station:weatherstation:save")
    public Result save(@RequestBody WeatherStationDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        WeatherStationEntity weatherStationEntity = BeanUtil.copyProperties(dto, WeatherStationEntity.class);
        weatherStationService.insert(weatherStationEntity);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("station:weatherstation:update")
    public Result<Boolean> update(@RequestBody WeatherStationDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        WeatherStationEntity weatherStationEntity = BeanUtil.copyProperties(dto, WeatherStationEntity.class);
        weatherStationService.updateById(weatherStationEntity);
        return new Result<Boolean>().ok(true);
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("station:weatherstation:delete")
    public Result<Boolean> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        weatherStationService.delete(ids);

        return new Result<Boolean>().ok(true);
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("station:weatherstation:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<WeatherStationDTO> list = weatherStationService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "气象站点表", list, WeatherStationExcel.class);
    }

}
