package com.zksy.web.controller.basicData;

import com.zksy.common.core.controller.BaseController;
import com.zksy.basicData.domain.XcrPatent;
import com.zksy.basicData.service.IXcrPatentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.basicData.utils.DowntemplateUtil;
import com.zksy.common.utils.SearchUtil;
import com.zksy.common.annotation.Log;
import com.zksy.common.enums.BusinessType;
import com.zksy.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

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.io.IOException;
import java.util.List;

/**
 * 专利Controller
 *
 * @author nahida
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/basicData/xcrPatent")
@Api(tags = "专利", description = "专利desc")
public class XcrPatentController extends BaseController{

    @Autowired
    private IXcrPatentService service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "专利搜索getById")
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:list')")
    public XcrPatent getById(@PathVariable String borrowId) {
        return service.getById(borrowId);
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "专利分页")
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:list')")
    public Page findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        return service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
    }

    @GetMapping("/getList")
    @ApiOperation(value = "专利查询所有")
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:list')")
    public List<XcrPatent> getList(String conditionJson) throws Exception {
        return service.list(SearchUtil.parseWhereSql(conditionJson));
    }

    /**
     * 新增专利
     */
    @PostMapping("/save")
    @ApiOperation(value = "专利新增")
    @Log(title = "新增专利", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody XcrPatent xcrPatent) {
        return service.save(xcrPatent);
    }

    /**
     * 修改专利
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "专利修改")
    @Log(title = "修改专利", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody XcrPatent xcrPatent) {
        return service.updateById(xcrPatent);
    }


    @Log(title = "专利", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除专利", notes = "删除专利")
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:delete')")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        return toAjax(service.removeByIds(ids));
    }

    /**
     * 导出专利列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:export')")
    @Log(title = "专利", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,String conditionJson) throws Exception
    {
        List<XcrPatent> list = service.list(SearchUtil.parseWhereSql(conditionJson));
        ExcelUtil<XcrPatent> util = new ExcelUtil<XcrPatent>(XcrPatent.class);
        util.exportExcel(response, list, "专利数据");
    }
    /**
     * 导入专利列表
     */
    @ApiOperation(value = "专利导入")
    @Log(title = "专利管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<XcrPatent> util = new ExcelUtil<XcrPatent>(XcrPatent.class);
        List<XcrPatent> list = util.importExcel(file.getInputStream());
        return toAjax(service.saveBatch(list));
    }

    @PostMapping("/getUploadTemplate")
    @ApiOperation(value = "获取上传模板", notes = "获取上传模板")
    public void getUploadTemplate(HttpServletResponse response) throws IOException, IOException {
        DowntemplateUtil.downloadTemplate(response,"专利模板");
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "清除全表", notes = "清除全表")
    @PreAuthorize("@ss.hasPermi('basicData:xcrPatent:delete')")
    @Log(title = "专利清除全表", businessType = BusinessType.DELETE)
    public AjaxResult deleteAll(){
        return service.deleteAll();
    }
}