package org.jeecg.modules.database.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.database.dto.NasaDataRecordPage;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @Description: 当期数据库记录
 * @Author: jeecg-boot
 * @Date: 2025-02-24
 * @Version: V1.0
 */
@Tag(name = "当期数据库记录")
@RestController
@RequestMapping("/database/localDatabase")
@Slf4j
public class LocalDatabaseController extends JeecgController<LocalDatabase, ILocalDatabaseService> {
    @Autowired
    private ILocalDatabaseService      localDatabaseService;
    @Autowired
    private INasaDataRecordService     nasaDataRecordService;
    @Autowired
    private IHistoricalDatabaseService historicalDatabaseService;

    /**
     * 分页列表查询
     *
     * @param localDatabase
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "当期数据库记录-分页列表查询")
    @Operation(summary = "当期数据库记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LocalDatabase>> queryPageList(LocalDatabase localDatabase,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<LocalDatabase> queryWrapper = QueryGenerator.initQueryWrapper(localDatabase, req.getParameterMap());
        Page<LocalDatabase> page = new Page<LocalDatabase>(pageNo, pageSize);
        IPage<LocalDatabase> pageList = localDatabaseService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @Operation(summary = "数据库全局搜索")
    @GetMapping(value = "/search")
    public Result<List<?>> search(@RequestParam(name = "kw") String keyWord, HttpServletRequest req) {
        List<LocalDatabase> localDatabaseList = localDatabaseService.search(keyWord);
        List<HistoricalDatabase> historicalDatabaseList = historicalDatabaseService.search(keyWord);
        List<NasaDataRecord> nasaDataRecordList = nasaDataRecordService.search(keyWord);

        List result = new ArrayList<>();

        result.addAll(localDatabaseList);
        result.addAll(historicalDatabaseList);
        result.addAll(nasaDataRecordList);

        return Result.OK(result);
    }

    /**
     * 添加
     *
     * @param localDatabase
     * @return
     */
    @AutoLog(value = "当期数据库记录-添加")
    @Operation(summary = "当期数据库记录-添加")
    @RequiresPermissions("database:local_database:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LocalDatabase localDatabase) {
        localDatabaseService.save(localDatabase);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param localDatabase
     * @return
     */
    @AutoLog(value = "当期数据库记录-编辑")
    @Operation(summary = "当期数据库记录-编辑")
    @RequiresPermissions("database:local_database:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LocalDatabase localDatabase) {
        localDatabaseService.updateById(localDatabase);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "当期数据库记录-通过id删除")
    @Operation(summary = "当期数据库记录-通过id删除")
    @RequiresPermissions("database:local_database:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        localDatabaseService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "当期数据库记录-批量删除")
    @Operation(summary = "当期数据库记录-批量删除")
    @RequiresPermissions("database:local_database:delete")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.localDatabaseService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "当期数据库记录-通过id查询")
    @Operation(summary = "当期数据库记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LocalDatabase> queryById(@RequestParam(name = "id", required = true) String id) {
        LocalDatabase localDatabase = localDatabaseService.getById(id);
        if (localDatabase == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(localDatabase);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param localDatabase
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LocalDatabase localDatabase) {
        return super.exportXls(request, localDatabase, LocalDatabase.class, "当期数据库记录");
    }

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

    @Operation(summary = "当期数据统计")
    @GetMapping(value = "/statistics")
    public Result<NasaDataRecordPage> statistics() {
        NasaDataRecordPage result = new NasaDataRecordPage();
        Long nasaCountLhs = nasaDataRecordService.sumLhs();
        Long nasaCountXhy = nasaDataRecordService.sumXhy();
        Long historicalCountLhs = historicalDatabaseService.sumLhs();
        Long historicalCountXhy = historicalDatabaseService.sumXhy();
        Long localCountLhs = localDatabaseService.sumLhs();
        Long localCountXhy = localDatabaseService.sumXhy();

        result.setLhsCount(localCountLhs);
        result.setLhsTotal(nasaCountLhs + localCountLhs + historicalCountLhs);
        result.setXhyCount(localCountXhy);
        result.setXyhTotal(nasaCountXhy + localCountXhy + historicalCountXhy);

        return Result.OK(result);
    }
}
