package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ss.feeRule.service.IFeeRuleAssembler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.feeRule.domain.FeeRule;
import com.ruoyi.ss.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.feeRule.domain.FeeRuleQuery;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收费模板Controller
 *
 * @author qzz
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/system/rule")
public class FeeRuleController extends BaseController
{
    @Autowired
    private IFeeRuleService feeRuleService;

    @Autowired
    private IFeeRuleAssembler feeRuleAssembler;

    /**
     * 查询收费模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(FeeRuleQuery query)
    {
        startPage();
        startOrderBy();
        List<FeeRuleVO> list = feeRuleService.selectEFeeRuleList(query);
        feeRuleAssembler.assembleRoomInfo(list);
        return getDataTable(list);
    }

    /**
     * 导出收费模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:export')")
    @Log(title = "收费模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeeRuleQuery query)
    {
        List<FeeRuleVO> list = feeRuleService.selectEFeeRuleList(query);
        ExcelUtil<FeeRuleVO> util = new ExcelUtil<FeeRuleVO>(FeeRuleVO.class);
        util.exportExcel(response, list, "收费模板数据");
    }

    /**
     * 获取收费模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId)
    {
        FeeRuleVO feeRuleVO = feeRuleService.selectEFeeRuleByRuleId(ruleId);
        feeRuleAssembler.assembleRoomInfo(new ArrayList<>(Collections.singletonList(feeRuleVO)));
        return success(feeRuleVO);
    }

    /**
     * 新增收费模板
     */
    @PreAuthorize("@ss.hasPermi('system:rule:add')")
    @Log(title = "收费模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeeRule eFeeRule)
    {
        return toAjax(feeRuleService.insertEFeeRule(eFeeRule));
    }

    /**
     * 修改收费模板
     */
    @PreAuthorize("@ss.hasPermi('system:rule:edit')")
    @Log(title = "收费模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeeRule eFeeRule)
    {
        return toAjax(feeRuleService.updateEFeeRule(eFeeRule));
    }

    /**
     * 删除收费模板
     */
    @PreAuthorize("@ss.hasPermi('system:rule:remove')")
    @Log(title = "收费模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds)
    {
        return toAjax(feeRuleService.deleteEFeeRuleByRuleIds(ruleIds));
    }
}
