package com.xhpc.general.controller;

import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.general.domain.XhpcAgreement;
import com.xhpc.general.service.IXhpcAgreementService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 协议管理controller
 * program: ruoyi
 * User: HongYun
 * Date:2021-08-03 09
 */
@RestController
@RequestMapping("/agreement")
public class XhpcAgreementController extends BaseController {

    @Autowired
    IXhpcAgreementService iXhpcAgreementService;
    @Autowired
    TokenService tokenService;

    @Log(title = "协议-删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody XhpcAgreement xhpcAgreement){
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        return iXhpcAgreementService.deleteAgreementItem(xhpcAgreement.getAgreementId(),tenantId);
    }

    @Log(title = "协议-添加", businessType = BusinessType.DELETE)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody XhpcAgreement xhpcAgreement){
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        xhpcAgreement.setTenantId(tenantId);
        return iXhpcAgreementService.insertAgreementItem(xhpcAgreement.getTitle(),xhpcAgreement.getType(),xhpcAgreement.getContent(),tenantId);
    }

    @Log(title = "协议-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhpcAgreement xhpcAgreement){
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        xhpcAgreement.setTenantId(tenantId);
        return iXhpcAgreementService.updateAgreementItem(xhpcAgreement.getAgreementId(),xhpcAgreement.getTitle(),xhpcAgreement.getType(),xhpcAgreement.getContent(),tenantId);
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll( String title,String createBy,Integer type){
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        startPage();
        return getDataTable(iXhpcAgreementService.selectAgreementItems(title,createBy,type,tenantId));
    }

    @GetMapping("/listOne")
    public AjaxResult listOne(@RequestParam Long agreementId){

        return AjaxResult.success(iXhpcAgreementService.selectAgreementItem(agreementId));
    }

    @GetMapping("/getContent")
    public AjaxResult getContent(Integer type,String tenantId){

        return AjaxResult.success(iXhpcAgreementService.selectContent(type,tenantId));
    }

}
