package com.hw.ems.base.controller;

import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.ems.base.domain.BaseTypeRelation;
import com.hw.ems.base.service.IBaseTypeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

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


/**
 * 计量与类型对应关系Controller
 *
 * @author YinQ
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/base/typeRelation")
public class BaseTypeRelationController extends BaseController {
    private String prefix = "base/typeRelation";

    @Autowired
    private IBaseTypeRelationService baseTypeRelationService;

    @RequiresPermissions("base:typeRelation:view")
    @GetMapping()
    public String typeRelation() {
        return prefix + "/typeRelation";
    }

    /**
     * 查询计量与类型对应关系列表
     */
    @RequiresPermissions("base:typeRelation:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseTypeRelation baseTypeRelation) {
        startPage();
        List<BaseTypeRelation> list = baseTypeRelationService.selectBaseTypeRelationList(baseTypeRelation);
        return getDataTable(list);
    }

    /**
     * 导出计量与类型对应关系列表
     */
    @RequiresPermissions("base:typeRelation:export")
    @Log(title = "计量与类型对应关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseTypeRelation baseTypeRelation) {
        List<BaseTypeRelation> list = baseTypeRelationService.selectBaseTypeRelationList(baseTypeRelation);
        ExcelUtil<BaseTypeRelation> util = new ExcelUtil<BaseTypeRelation>(BaseTypeRelation.class);
        util.exportExcel(response, list, "计量与类型对应关系数据");
    }

    /**
     * 新增计量与类型对应关系
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存计量与类型对应关系
     */
    @RequiresPermissions("base:typeRelation:add")
    @Log(title = "计量与类型对应关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(BaseTypeRelation baseTypeRelation) {
        return toAjax(baseTypeRelationService.insertBaseTypeRelation(baseTypeRelation));
    }

    /**
     * 修改计量与类型对应关系
     */
    @RequiresPermissions("base:typeRelation:edit")
    @GetMapping("/edit/{objid}")
    public String edit(@PathVariable("objid") Long objid, ModelMap mmap) {
        BaseTypeRelation baseTypeRelation = baseTypeRelationService.selectBaseTypeRelationByObjid(objid);
        mmap.put("baseTypeRelation", baseTypeRelation);
        return prefix + "/edit";
    }

    /**
     * 修改保存计量与类型对应关系
     */
    @RequiresPermissions("base:typeRelation:edit")
    @Log(title = "计量与类型对应关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(BaseTypeRelation baseTypeRelation) {
        return toAjax(baseTypeRelationService.updateBaseTypeRelation(baseTypeRelation));
    }

    /**
     * 删除计量与类型对应关系
     */
    @RequiresPermissions("base:typeRelation:remove")
    @Log(title = "计量与类型对应关系", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(String ids) {
        return toAjax(baseTypeRelationService.deleteBaseTypeRelationByObjids(ids));
    }
}
