package org.jeecg.modules.database.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.database.entity.SearchResult;
import org.jeecg.modules.database.service.IComponentSearchService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
@Tag(name = "样品元器件模糊预搜索API")
@RestController
@RequestMapping("/database/componentSearch")
@Slf4j
public class ComponentSearchController extends JeecgController<SearchResult, IComponentSearchService> {
    @Autowired
    private IComponentSearchService componentSearchService;

    @Autowired
    private ISysUserService userService;

    /**
     * 默认查询器件类型的分页列表
     * @param type
     * @param content
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "器件查询 - 分页查询API")
    @GetMapping(value = "/list")
    public Result<IPage<SearchResult>> queryPageList(@RequestParam(name = "type", defaultValue = "3") Integer type,@RequestParam(name = "content", defaultValue = "") String content,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Result<IPage<SearchResult>> result = new Result<>();
        IPage<SearchResult> pageList = componentSearchService.queryPageList(type, content, pageNo, pageSize);
        result.setSuccess(true);
        result.setResult(pageList);

        return result;
    }

    /**
     * 模糊搜索接口
     * @param type
     * @param content
     * @return
     */
    @Operation(summary = "器件查询 - 分页查询API")
    @GetMapping(value = "/search")
    public Result<List<SearchResult>> search(@RequestParam(name = "type", defaultValue = "") Integer type,@RequestParam(name = "content", defaultValue = "") String content) {
        Result<List<SearchResult>> result = new Result<>();
        result.setSuccess(true);
        result.setResult(componentSearchService.search(type, content));

        return result;
    }

    @AutoLog(value = "器件搜索数据-添加")
    @Operation(summary = "器件搜索数据-添加")
    // @RequiresPermissions("database:document_favorites:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SearchResult searchResult, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);

        Date currentDate = new Date();
        searchResult.setCreateTime(currentDate);
        searchResult.setUpdateTime(currentDate);
        searchResult.setCreateBy(userByName.getId());
        searchResult.setUpdateBy(userByName.getId());
        try {
            componentSearchService.saveComponentSearch(searchResult);
        }catch (JeecgBootException e) {
            return Result.error(e.getMessage());
        }

        return Result.OK("保存成功！");
    }


    /**
     * 编辑
     * @param searchResult
     * @return
     */
    @AutoLog(value = "器件搜索数据-编辑")
    @Operation(summary = "器件搜索数据-编辑")
    // @RequiresPermissions("database:document_favorites:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SearchResult searchResult, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);
        searchResult.setUpdateBy(userByName.getId());
        searchResult.setUpdateTime(new Date());
        try {
            componentSearchService.saveComponentSearch(searchResult);
        }catch (JeecgBootException e) {
            return Result.error(e.getMessage());
        }

        return Result.OK("编辑成功!");
    }



    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "器件搜索数据-通过id删除")
    @Operation(summary = "器件搜索数据-通过id删除")
   //  @RequiresPermissions("database:document_favorites:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        componentSearchService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param result
     */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SearchResult result) {
        return super.exportXls(request, result, SearchResult.class, "样品类型信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
   // @RequiresPermissions("database:experiment:add")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SearchResult.class);
    }

}
