package com.ruoyi.web.bst;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.bonus.domain.Bonus;
import com.ruoyi.bst.bonus.domain.BonusQuery;
import com.ruoyi.bst.bonus.domain.BonusVO;
import com.ruoyi.bst.bonus.domain.enums.BonusArrivalType;
import com.ruoyi.bst.bonus.service.BonusService;
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.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 分成明细Controller
 *
 * @author ruoyi
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/bst/bonus")
public class BonusController extends BaseController
{
    @Autowired
    private BonusService bonusService;

    /**
     * 查询分成明细列表
     */
    @PreAuthorize("@ss.hasPermi('bst:bonus:list')")
    @GetMapping("/list")
    public TableDataInfo list(BonusQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<BonusVO> list = bonusService.selectBonusList(query);
        return getDataTable(list);
    }

    /**
     * 导出分成明细列表
     */
    @PreAuthorize("@ss.hasPermi('bst:bonus:export')")
    @Log(title = "分成明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BonusQuery query)
    {
        query.setScope(true);
        List<BonusVO> list = bonusService.selectBonusList(query);
        ExcelUtil<BonusVO> util = new ExcelUtil<BonusVO>(BonusVO.class);
        util.exportExcel(response, list, "分成明细数据");
    }

    /**
     * 获取分成明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:bonus:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bonusService.selectBonusById(id, true));
    }

    /**
     * 预览设备分成信息
     */
    @PreAuthorize("@ss.hasPermi('bst:bonus:preview')")
    @GetMapping(value = "/preview")
    public AjaxResult preview(Long deviceId) {
        List<Bonus> list = bonusService.preview(deviceId);
        // 若不是系统管理员，则只返回当前用户可见信息
        if (!isSysAdmin()) {
            Long userId = getUserId();
            list = list.stream().filter(bonus ->
                BonusArrivalType.userList().contains(bonus.getArrivalType())
                && bonus.getViewUserIds() != null
                && bonus.getViewUserIds().contains(userId)
            ).collect(Collectors.toList());
        }
        return success(list);
    }

    /**
     * 支付分成
     */
    @PreAuthorize("@ss.hasPermi('bst:bonus:pay')")
    @PutMapping("/pay")
    @Log(title = "支付分成", businessType = BusinessType.OTHER, bizIdName = "arg0", bizType = LogBizType.BONUS)
    public AjaxResult pay(Long id) {
        return success(bonusService.payBonus(id));
    }
}
