package com.ylx.web.controller.massage;


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.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.domain.TFareSetting;
import com.ylx.massage.domain.TFareFreeRule;
import com.ylx.massage.domain.vo.DisableFareFreeRuleVo;
import com.ylx.massage.domain.vo.FareFreeRuleVo;
import com.ylx.massage.service.TFareSettingService;
import com.ylx.massage.service.TFareFreeRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 车费设置表(TFareSetting)控制层
 *
 * @author makejava
 * @since 2024-08-22 17:41:00
 */
@RestController
@Api(tags = {"车费设置"})
@RequestMapping("tFareSetting")
public class TFareSettingController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TFareSettingService tFareSettingService;

    /**
     * 免车费规则服务
     */
    @Resource
    private TFareFreeRuleService fareFreeRuleService;

    /**
     * 分页查询所有车费设置数据
     *
     * @param page         分页对象
     * @param tFareSetting 查询实体
     * @return R 所有数据
     */
    @GetMapping("selectAll")
    @ApiOperation("分页查询数据")
    public R selectAll(Page<TFareSetting> page, TFareSetting tFareSetting) {
        try {
            LambdaQueryWrapper<TFareSetting> queryWrapper = new LambdaQueryWrapper<>();
            // 城市编码查询
            if(StringUtils.isNotBlank(tFareSetting.getCityCode())){
                queryWrapper.like(TFareSetting::getCityCode, tFareSetting.getCityCode());
            }
            return R.ok(this.tFareSettingService.page(page, queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过主键查询车费设置详情
     *
     * @param id 主键
     * @return 车费设置详情
     */
    @GetMapping("{id}")
    @ApiOperation("车费设置详情")
    public R selectOne(@PathVariable Serializable id) {
        try {
            return R.ok(this.tFareSettingService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增车费设置数据
     *
     * @param tFareSetting 实体对象
     * @return R 新增结果
     */
    @PostMapping("add")
    @ApiOperation("新增数据")
    public R insert(@RequestBody TFareSetting tFareSetting) {
        try {
            return R.ok(this.tFareSettingService.add(tFareSetting));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改车费设置数据
     *
     * @param tFareSetting 实体对象
     * @return R 修改结果
     */
    @PostMapping("update")
    @ApiOperation("修改车费设置数据")
    public R update(@RequestBody TFareSetting tFareSetting) {
        try {
            return R.ok(this.tFareSettingService.updateFareSetting(tFareSetting));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据主键ID删除车费设置数据
     *
     * @param id 主键
     * @return R 删除结果
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation("根据主键ID删除车费设置数据")
    public R delete(@PathVariable String id) {
        try {
            return R.ok(this.tFareSettingService.removeById(id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除车费设置数据
     *
     * @param idList 主键结合
     * @return R 删除结果
     */
    @PostMapping("delete")
    @ApiOperation("批量删除车费设置数据")
    public R delete(@RequestBody List<String> idList) {
        try {
            return R.ok(this.tFareSettingService.removeByIds(idList));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存免车费规则（H5 技师端）
     * 支持新增和修改操作
     * - 如果传入id则修改
     * - 如果不传id则新增
     * <p>
     * 业务流程：
     * 1. 接收前端传递的免车费规则参数
     * 2. 调用服务层保存方法（自动判断新增或修改）
     * 3. 返回操作结果
     *
     * @param fareFreeRule 免车费规则（包含id则为修改，不包含则为新增）
     * @return R
     */
    @PostMapping("fareFreeRule/add")
    @ApiOperation("保存免车费规则（支持新增和修改）")
    public R saveFareFreeRule(@RequestBody TFareFreeRule fareFreeRule) {
        try {
            logger.info("保存免车费规则，前端传递的参数：{}", JSON.toJSONString(fareFreeRule));
            boolean success = fareFreeRuleService.saveFareFreeRule(fareFreeRule);

            // 根据是否包含id判断是新增还是修改
            String message = StringUtils.isNotBlank(fareFreeRule.getId()) ? "修改成功" : "新增成功";
            return success ? R.ok(message) : R.fail("操作失败");
        } catch (Exception e) {
            logger.error("保存免车费规则失败", e);
            return R.fail(e.getMessage());
        }
    }


    /**
     * 禁用（启用）免车费规则（H5 技师端）
     *
     * @param disableFareFreeRuleVo
     * @return R
     */
    @PostMapping("fareFreeRule/disable")
    @ApiOperation("禁用（启用）免车费规则")
    public R disableFareFreeRule(@RequestBody DisableFareFreeRuleVo disableFareFreeRuleVo) {
        try {
            boolean success = fareFreeRuleService.disableFareFreeRule(disableFareFreeRuleVo.getOpenId(), disableFareFreeRuleVo.getEnable());
            return success ? R.ok() : R.fail("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }


    /**
     * 查询免车费规则列表
     *
     * @param openId 技师OpenID
     * @return R
     */
    @GetMapping("fareFreeRule/list")
    @ApiOperation("查询免车费规则列表")
    public R listFareFreeRules(@RequestParam String openId) {
        try {
            FareFreeRuleVo fareFreeRuleVo = fareFreeRuleService.listFareFreeRules(openId);
            return R.ok(fareFreeRuleVo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除免车费规则
     *
     * @param idList 规则ID列表
     * @return R
     */
    @PostMapping("fareFreeRule/delete")
    @ApiOperation("删除免车费规则")
    public R deleteFareFreeRule(@RequestBody List<String> idList) {
        try {
            boolean success = fareFreeRuleService.removeByIds(idList);
            return success ? R.ok("删除成功") : R.fail("删除失败");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


}

