package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bst.suit.domain.enums.SuitRentalUnit;
import com.ruoyi.bst.suit.domain.enums.SuitRidingRule;
import com.ruoyi.bst.suit.utils.SuitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.suit.domain.Suit;
import com.ruoyi.bst.suit.domain.SuitQuery;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitService;
import com.ruoyi.bst.suit.service.SuitValidator;
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.page.TableDataInfo;
import com.ruoyi.common.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserValidator;

/**
 * 套餐Controller
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/bst/suit")
public class SuitController extends BaseController
{
    @Autowired
    private SuitService suitService;

    @Autowired
    private SuitValidator suitValidator;

    @Autowired
    private UserValidator userValidator;

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuitQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        return getDataTable(list);
    }

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:list')")
    @PostMapping("/listByIds")
    public AjaxResult listByIds(@RequestBody List<Long> ids)
    {
        SuitQuery query = new SuitQuery();
        query.setIds(ids);
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        return success(list);
    }

    /**
     * 导出套餐列表
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:export')")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuitQuery query)
    {
        query.setScope(true);
        List<SuitVO> list = suitService.selectSuitList(query);
        ExcelUtil<SuitVO> util = new ExcelUtil<SuitVO>(SuitVO.class);
        util.exportExcel(response, list, "套餐数据");
    }

    /**
     * 获取套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(suitService.selectSuitById(id, true));
    }

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:add')")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Suit suit)
    {
        // 判断能否操作他人的套餐，不能则设置为当前用户
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(getUserId());
        }
        return toAjax(suitService.insertSuit(suit));
    }

    /**
     * 修改套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:edit')")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Suit suit) {

        ServiceUtil.assertion(!suitValidator.canEdit(suit.getId()),
        "您无权修改ID为%s的套餐", suit.getId());
        // 若不能操作他人的套餐，则不修改用户ID
        if (!userValidator.canView(suit.getUserId())) {
            suit.setUserId(null);
        }
        return toAjax(suitService.updateSuit(suit));
    }

    /**
     * 删除套餐
     */
    @PreAuthorize("@ss.hasPermi('bst:suit:remove')")
    @Log(title = "套餐", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(suitService.deleteSuitByIds(ids));
    }

//    @GetMapping("/test")
//    public AjaxResult test(Long seconds, Long suitId)
//    {
//        SuitVO suit = suitService.selectSuitById(suitId);
//        SuitRentalUnit rentalUnit = SuitRentalUnit.parse(suit.getRentalUnit());
//        if (SuitRidingRule.INTERVAL_FEE.getCode().equals(suit.getRidingRule())) {
//            return success(SuitUtil.calcAmountForIntervalFee(seconds, suit.getIntervalRule(), rentalUnit));
//        } else {
//            return success(SuitUtil.calcAmountForStartFee(seconds, suit.getStartRule(), rentalUnit));
//        }
//    }
}
