package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.BathFacilityService;
import shop.alien.entity.store.vo.BathFacilityServiceCategoryVo;
import shop.alien.entity.store.vo.BathFacilityServiceVo;
import shop.alien.store.service.BathFacilityServiceService;

import java.util.List;

/**
 * 洗浴设施及服务Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"洗浴设施及服务管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/bath/facility/service")
@RequiredArgsConstructor
public class BathFacilityServiceController {

    private final BathFacilityServiceService facilityServiceService;

    @ApiOperation("分页查询洗浴设施及服务列表（用户端）")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", required = true, defaultValue = "10"),
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "facilityCategory", value = "设施分类(1:洗浴区, 2:汗蒸区, 3:休闲区, 4:餐饮区)", dataType = "int", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<BathFacilityServiceVo>> getPageList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam Integer storeId,
            @RequestParam(required = false) Integer facilityCategory) {
        log.info("BathFacilityServiceController.getPageList?pageNum={},pageSize={},storeId={},facilityCategory={}",
                pageNum, pageSize, storeId, facilityCategory);
        return R.data(facilityServiceService.getPageList(pageNum, pageSize, storeId, facilityCategory));
    }

    @ApiOperation("查询洗浴设施及服务列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "facilityCategory", value = "设施分类(1:洗浴区, 2:汗蒸区, 3:休闲区, 4:餐饮区)", dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public R<List<BathFacilityServiceVo>> getList(
            @RequestParam Integer storeId,
            @RequestParam(required = false) Integer facilityCategory) {
        log.info("BathFacilityServiceController.getList?storeId={},facilityCategory={}", storeId, facilityCategory);
        return R.data(facilityServiceService.getList(storeId, facilityCategory));
    }

    @ApiOperation("根据ID查询洗浴设施及服务详情（用户端）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<BathFacilityServiceVo> getDetail(@RequestParam Integer id) {
        log.info("BathFacilityServiceController.getDetail?id={}", id);
        BathFacilityServiceVo vo = facilityServiceService.getDetail(id);
        if (vo == null) {
            return R.fail("数据不存在");
        }
        return R.data(vo);
    }

    @ApiOperation("新增洗浴设施及服务")
    @ApiOperationSupport(order = 4)
    @PostMapping("/save")
    public R<Boolean> saveFacilityService(@RequestBody BathFacilityServiceVo vo) {
        log.info("BathFacilityServiceController.saveFacilityService?vo={}", vo);
        try {
            BathFacilityService facilityService = new BathFacilityService();
            facilityService.setStoreId(vo.getStoreId());
            facilityService.setFacilityCategory(vo.getFacilityCategory());
            facilityService.setFacilityName(vo.getFacilityName());
            facilityService.setChargingStandard(vo.getChargingStandard());
            facilityService.setUsageTimeType(vo.getUsageTimeType());
            facilityService.setUsageStartTime(vo.getUsageStartTime());
            facilityService.setUsageEndTime(vo.getUsageEndTime());
            facilityService.setDisplayInStoreDetail(vo.getDisplayInStoreDetail());
            boolean result = facilityServiceService.saveFacilityService(facilityService, vo.getImageList());
            if (result) {
                return R.success("新增成功");
            }
            return R.fail("新增失败");
        } catch (Exception e) {
            log.error("BathFacilityServiceController.saveFacilityService异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("修改洗浴设施及服务")
    @ApiOperationSupport(order = 5)
    @PostMapping("/update")
    public R<Boolean> updateFacilityService(@RequestBody BathFacilityServiceVo vo) {
        log.info("BathFacilityServiceController.updateFacilityService?vo={}", vo);
        try {
            if (vo.getId() == null) {
                return R.fail("主键ID不能为空");
            }
            BathFacilityService facilityService = new BathFacilityService();
            facilityService.setId(vo.getId());
            facilityService.setStoreId(vo.getStoreId());
            facilityService.setFacilityCategory(vo.getFacilityCategory());
            facilityService.setFacilityName(vo.getFacilityName());
            facilityService.setChargingStandard(vo.getChargingStandard());
            facilityService.setUsageTimeType(vo.getUsageTimeType());
            facilityService.setUsageStartTime(vo.getUsageStartTime());
            facilityService.setUsageEndTime(vo.getUsageEndTime());
            facilityService.setDisplayInStoreDetail(vo.getDisplayInStoreDetail());
            boolean result = facilityServiceService.updateFacilityService(facilityService, vo.getImageList());
            if (result) {
                return R.success("修改成功");
            }
            return R.fail("修改失败");
        } catch (Exception e) {
            log.error("BathFacilityServiceController.updateFacilityService异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除洗浴设施及服务")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/delete")
    public R<Boolean> deleteFacilityService(@RequestParam Integer id) {
        log.info("BathFacilityServiceController.deleteFacilityService?id={}", id);
        try {
            boolean result = facilityServiceService.deleteFacilityService(id);
            if (result) {
                return R.success("删除成功");
            }
            return R.fail("删除失败");
        } catch (Exception e) {
            log.error("BathFacilityServiceController.deleteFacilityService异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("查询指定店铺按分类汇总的设备信息（包含设备数量、设备列表和图片）（用户端）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/categorySummary")
    public R<List<BathFacilityServiceCategoryVo>> getCategorySummary(@RequestParam Integer storeId) {
        log.info("BathFacilityServiceController.getCategorySummary?storeId={}", storeId);
        return R.data(facilityServiceService.getCategorySummary(storeId));
    }


    @ApiOperation("分页查询洗浴设施及服务列表（商户端）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", required = true, defaultValue = "10"),
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "facilityCategory", value = "设施分类(1:洗浴区, 2:汗蒸区, 3:休闲区, 4:餐饮区)", dataType = "int", paramType = "query")
    })
    @GetMapping("/storePage")
    public R<IPage<BathFacilityServiceVo>> getStorePageList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam Integer storeId,
            @RequestParam(required = false) Integer facilityCategory) {
        log.info("BathFacilityServiceController.getStorePageList?pageNum={},pageSize={},storeId={},facilityCategory={}",
                pageNum, pageSize, storeId, facilityCategory);
        return R.data(facilityServiceService.getStorePageList(pageNum, pageSize, storeId, facilityCategory));
    }

    @ApiOperation("根据ID查询洗浴设施及服务详情（商户端）")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/storeDetail")
    public R<BathFacilityServiceVo> getStoreDetail(@RequestParam Integer id) {
        log.info("BathFacilityServiceController.getStoreDetail?id={}", id);
        BathFacilityServiceVo vo = facilityServiceService.getStoreDetail(id);
        if (vo == null) {
            return R.fail("数据不存在");
        }
        return R.data(vo);
    }

    @ApiOperation("查询指定店铺按分类汇总的设备信息（包含设备数量、设备列表和图片）（商户端）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/storeCategorySummary")
    public R<List<BathFacilityServiceCategoryVo>> getStoreCategorySummary(@RequestParam Integer storeId) {
        log.info("BathFacilityServiceController.getStoreCategorySummary?storeId={}", storeId);
        return R.data(facilityServiceService.getStoreCategorySummary(storeId));
    }
}

