package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.feeRule.domain.FeeRule;
import com.ruoyi.ss.feeRule.domain.FeeRuleDTO;
import com.ruoyi.ss.feeRule.domain.FeeRuleQuery;
import com.ruoyi.ss.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.feeRule.service.IFeeRuleAssembler;
import com.ruoyi.ss.feeRule.service.IFeeRuleConverter;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import com.ruoyi.ss.feeRule.service.IFeeRuleValidator;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.service.IRoomService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 套餐Controller
 * @author qzz
 * 2024/3/5
 */
@Api(tags = "套餐")
@RestController
@RequestMapping("/app/rule")
public class AppRuleController extends BaseController {

    @Autowired
    private IFeeRuleService feeRuleService;

    @Resource
    private IFeeRuleConverter feeRuleConverter;

    @Autowired
    private IRoomService roomService;

    @Resource
    private IFeeRuleValidator feeRuleValidator;

    @Autowired
    private IFeeRuleAssembler feeRuleAssembler;


    /**
     * 套餐列表
     */
    @GetMapping("/list")
    public AjaxResult list(FeeRuleQuery query)
    {
        logger.info("套餐列表：【feeRuleQuery={}】", JSON.toJSONString(query));
        if(query.getMerchantId() == null) {
            try {
                query.setMerchantId(SecurityUtils.getUserId());
            } catch (Exception e) {
                logger.info("获取用户ID失败", e);
            }
        }
        List<FeeRuleVO> list = feeRuleService.selectEFeeRuleList(query);
        return success(list);
    }

    /**
     * 根据房间id获取套餐
     */
    @GetMapping("/getListByRoomId")
    public AjaxResult ruleList(Long roomId)
    {
        ServiceUtil.assertion(roomId == null, "房间id不能为空");
        logger.info("根据房间id获取套餐：【roomId={}】", roomId);
        FeeRuleQuery feeRuleQuery = new FeeRuleQuery();
        feeRuleQuery.setRoomId(roomId);
        List<FeeRuleVO> list = feeRuleService.selectEFeeRuleList(feeRuleQuery);
        return success(list);
    }

    /**
     * 新增收费模板
     */
    @Log(title = "新增收费模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated({ValidGroup.Create.class}) FeeRuleDTO eFeeRule)
    {
        logger.info("新增收费模板：【{}】", JSON.toJSONString(eFeeRule));
        FeeRule rule = feeRuleConverter.toOrderBO(eFeeRule);
        return toAjax(feeRuleService.insertEFeeRule(rule));
    }

    /**
     * 修改收费模板
     */
    @Log(title = "修改收费模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editRule(@RequestBody @Validated({ValidGroup.Update.class}) FeeRuleDTO rlFeeRule)
    {
        logger.info("修改收费模板：【{}】", JSON.toJSONString(rlFeeRule));
        FeeRule rule = feeRuleConverter.toOrderBO(rlFeeRule);
        return toAjax(feeRuleService.updateEFeeRule(rule));
    }

    /**
     * 删除收费模板
     */
    @Log(title = "收费模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds)
    {
        logger.info("删除收费模板：【{}】", JSON.toJSONString(ruleIds));
        feeRuleValidator.preDelRule(ruleIds);
        return toAjax(feeRuleService.deleteEFeeRuleByRuleIds(ruleIds));
    }

    /**
     * 获取收费模板详细信息
     */
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId)
    {
        logger.info("获取收费模板详细信息：【{}】", ruleId);
        ServiceUtil.assertion(ruleId == null, "收费模板id不能为空");
        FeeRuleVO feeRuleVO = feeRuleService.selectEFeeRuleByRuleId(ruleId);
        feeRuleAssembler.assembleRoomInfo(new ArrayList<>(Collections.singletonList(feeRuleVO)));
        return success(feeRuleVO);
    }

}
