package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

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.agreement.domain.Agreement;
import com.ruoyi.bst.agreement.domain.AgreementQuery;
import com.ruoyi.bst.agreement.domain.AgreementVO;
import com.ruoyi.bst.agreement.service.AgreementConverter;
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.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 协议Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/bst/agreement")
public class AgreementController extends BaseController
{
    @Autowired
    private AgreementService agreementService;
    @Autowired
    private AgreementValidator agreementValidator;
    @Autowired
    private AgreementConverter agreementConverter;

    /**
     * 查询协议列表
     */
    @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, bizIdName = "arg0", bizType = LogBizType.AGREEMENT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Agreement agreement)
    {
        agreementConverter.toPoByCreate(agreement);
        return toAjax(agreementService.insertAgreement(agreement));
    }

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

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