package com.ylx.massage.controller;



import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.common.utils.SecurityUtils;
import com.ylx.massage.domain.Area;
import com.ylx.massage.domain.CityOperationApplication;
import com.ylx.massage.service.CityOperationApplicationService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 城市管理表(CityOperationApplication)表控制层
 *
 * @author makejava
 * @since 2026-06-04 14:19:49
 */
@RestController
@RequestMapping("cityOperationApplication")
@Slf4j
public class CityOperationApplicationController {
    /**
     * 服务对象
     */
    @Autowired
    private CityOperationApplicationService cityOperationApplicationService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param cityOperationApplication 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public R selectAll(Page<CityOperationApplication> page, CityOperationApplication cityOperationApplication) {
        return R.ok(this.cityOperationApplicationService.page(page, new QueryWrapper<>(cityOperationApplication)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.cityOperationApplicationService.getById(id));
    }

    /**
     * 申请开通新城市
     *
     * @param cityOperationApplication 实体对象
     * @return R 新增结果
     */
    @PostMapping("/saveCity")
    public R insert(@RequestBody CityOperationApplication cityOperationApplication) {
        try {
            log.info("申请开通新城市: {}", JSON.toJSONString(cityOperationApplication));
            // 获取当前登录的用户
            String nickName = SecurityUtils.getWxLoginUser().getCNickName();
            cityOperationApplication.setCreateBy(nickName);
            cityOperationApplication.setCreateTime(LocalDateTime.now());
            return R.ok(cityOperationApplicationService.save(cityOperationApplication));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据商户ID查询城市管理列表
     * @param merchantId
     * @return R
     */
    @GetMapping("/getCityList")
    @ApiOperation("根据商户ID查询城市管理列表")
    public R getCityList(@RequestParam Integer merchantId) {
        try {
            LambdaQueryWrapper<CityOperationApplication> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CityOperationApplication::getMerchantId, merchantId);
            List<CityOperationApplication> list = cityOperationApplicationService.list(queryWrapper);
            if (CollectionUtil.isEmpty(list)) {
                return R.fail("未找到此数据");
            }
            return R.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }



}

