package com.ruoyi.web.bst;

import com.ruoyi.bst.agreement.domain.Agreement;
import com.ruoyi.bst.agreement.domain.AgreementQuery;
import com.ruoyi.bst.agreement.domain.AgreementVO;
import com.ruoyi.bst.agreement.service.AgreementService;
import com.ruoyi.bst.agreement.service.AgreementValidator;
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 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 2025-04-07
 */
@RestController
@RequestMapping("/bst/agreement")
public class AgreementController extends BaseController
{
    @Autowired
    private AgreementService agreementService;
    @Autowired
    private AgreementValidator agreementValidator;

    /**
     * 查询协议列表
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgreementQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<AgreementVO> list = agreementService.selectAgreementList(query);
        return getDataTable(list);
    }

    /**
     * 导出协议列表
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:export')")
    @Log(title = "协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgreementQuery query)
    {
        List<AgreementVO> list = agreementService.selectAgreementList(query);
        ExcelUtil<AgreementVO> util = new ExcelUtil<AgreementVO>(AgreementVO.class);
        util.exportExcel(response, list, "协议数据");
    }

    /**
     * 获取协议详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(agreementService.selectAgreementById(id));
    }

    /**
     * 新增协议
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:add')")
    @Log(title = "协议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgreementQuery agreement)
    {
        agreement.setUserId(getUserId());
        return toAjax(agreementService.insertAgreement(agreement));
    }

    /**
     * 修改协议
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:edit')")
    @Log(title = "协议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agreement agreement)
    {
        if (!agreementValidator.canEdit(agreement.getId())) {
            return AjaxResult.error("您没有权限修改ID为" + agreement.getId() + "的协议");
        }
        return toAjax(agreementService.updateAgreement(agreement));
    }

    /**
     * 删除协议
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:remove')")
    @Log(title = "协议", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        if (!agreementValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您没有权限删除ID为" + ids + "的协议");
        }
        return toAjax(agreementService.deleteAgreementByIds(ids));
    }
}
