package com.ruoyi.web.controller.ss;

import com.ruoyi.common.annotation.DataScope;
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.utils.poi.ExcelUtil;
import com.ruoyi.ss.bonus.domain.BonusQuery;
import com.ruoyi.ss.bonus.domain.BonusVO;
import com.ruoyi.ss.bonus.service.BonusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分成明细Controller
 *
 * @author ruoyi
 * @date 2024-08-20
 */
@RestController
@RequestMapping("/ss/bonus")
public class BonusController extends BaseController
{
    @Autowired
    private BonusService bonusService;

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

    /**
     * 查询根据订单查询所有分成明细列表
     */
    @DataScope(deptAlias = "sd")
    @PreAuthorize("@ss.hasPermi('ss:bonus:list')")
    @GetMapping("/byBill")
    public AjaxResult listByBill(@RequestParam Long billId) {
        BonusQuery query = new BonusQuery();
        query.setBillId(billId);
//        query.setNeedScope(true);
        List<BonusVO> list = bonusService.selectBonusList(query);
        return success(list);
    }

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

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

    @Log(title = "支付分成", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('ss:bonus:pay')")
    @PutMapping("/{id}/pay")
    public AjaxResult pay(@PathVariable Long id) {
        return toAjax(bonusService.payBonusById(id));
    }

}
