package com.bonus.sgzb.material.controller;

import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.material.domain.AgreementInfo;
import com.bonus.sgzb.material.service.AgreementInfoService;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lsun
 */
@Api(tags = " 协议管理")
@RestController
@RequestMapping("/agreementInfo")
public class AgreementInfoController extends BaseController {
    @Autowired
    private AgreementInfoService agreementInfoService;

    /**
     * 获取协议管理列表
     */
    @ApiOperation(value = "获取协议管理列表")
    @GetMapping("/getAgreementInfoAll")
    public TableDataInfo getAgreementInfoAll(AgreementInfo bean) {
        startPage();
        List<AgreementInfo> list = agreementInfoService.getAgreementInfoAll(bean);
        return getDataTable(list);
    }

    /**
     * 获取协议管理列单个信息
     */
    @ApiOperation(value = "获取协议管理列单个信息")
    @GetMapping(value = "/getAgreementInfoId")
    public AjaxResult getAgreementInfoId(AgreementInfo bean) {
        AgreementInfo agreementInfo = agreementInfoService.getAgreementInfoId(bean);
        return success(agreementInfo);
    }

    /**
     * 往来单位
     */
    @ApiOperation(value = "往来单位")
    @GetMapping("/getUnitList")
    public AjaxResult getUnitList() {
        List<AgreementInfo> list = agreementInfoService.getUnitList();
        return AjaxResult.success(list);
    }

    /**
     * 工程名称
     */
    @ApiOperation(value = "工程名称")
    @GetMapping("/getProjectList")
    public AjaxResult getProjectList() {
        List<AgreementInfo> list = agreementInfoService.getProjectList();
        return AjaxResult.success(list);
    }

    @ApiOperation("协议管理-保存")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AgreementInfo bean) {
        int i = agreementInfoService.add(bean);
        if (i == -1) {
            return AjaxResult.error("已存在重复数据");
        } else {
            return toAjax(i);
        }
    }

    @ApiOperation("协议管理-修改")
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody AgreementInfo bean) {
        return toAjax(agreementInfoService.update(bean));
    }


    /**
     * 删除库管员配置
     */
    @ApiOperation(value = "协议管理-删除")
    @PostMapping("/remove")
    public AjaxResult deleteByIds(@Validated @RequestBody AgreementInfo bean) {
        return agreementInfoService.deleteByIds(bean);
    }


    /**
     * 协议管理导出
     */
    @ApiOperation("协议管理导出")
    @Log(title = "协议管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgreementInfo agreementInfo) {
        List<AgreementInfo> list = agreementInfoService.exportList(agreementInfo);
        ExcelUtil<AgreementInfo> util = new ExcelUtil<AgreementInfo>(AgreementInfo.class);
        util.exportExcel(response, list, "协议管理数据");
    }
}
