package com.ruoyi.web.app;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.agreement.domain.AgreementQuery;
import com.ruoyi.bst.agreement.domain.AgreementVO;
import com.ruoyi.bst.agreement.domain.enums.AgreementContentType;
import com.ruoyi.bst.agreement.service.AgreementService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.collection.CollectionUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app/agreement")
public class AppAgreementController extends BaseController {

    @Autowired
    private AgreementService agreementService;

    @ApiOperation("查询最新协议")
    @GetMapping("/latest")
    @Anonymous
    public AjaxResult getAgreement(AgreementQuery query) {
        PageHelper.startPage(1, 1);
        PageHelper.orderBy("create_time desc");
        query.setContentType(AgreementContentType.TEXT.getCode());
        if (query.getAgreementType()==null) {
            return AjaxResult.error("协议类型不能为空");
        }
        if (query.getAreaId()==null) {
            return AjaxResult.error("运营区不能为空");
        }
        AgreementVO agreementVO = CollectionUtils.firstElement(agreementService.selectAgreementList(query));
        return success(agreementVO);
    }


}
