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.AgreementConverter;
import com.ruoyi.bst.agreement.service.AgreementService;
import com.ruoyi.bst.userApp.service.UserAppValidator;
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-05-14
 */
@RestController
@RequestMapping("/bst/agreement")
public class AgreementController extends BaseController
{
    @Autowired
    private AgreementService agreementService;
    @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)
    @PostMapping
    public AjaxResult add(@RequestBody Agreement agreement)
    {
        agreementConverter.toPoByCreate(agreement);
        return toAjax(agreementService.insertAgreement(agreement));
    }

    /**
     * 修改协议列表
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:edit')")
    @Log(title = "协议列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agreement agreement)
    {
        agreementConverter.toPoByUpdate(agreement);
        return toAjax(agreementService.updateAgreement(agreement));
    }

    /**
     * 删除协议列表
     */
    @PreAuthorize("@ss.hasPermi('bst:agreement:remove')")
    @Log(title = "协议列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(agreementService.deleteAgreementByIds(ids));
    }

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