package org.jeecg.modules.oroqen.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 lombok.extern.slf4j.Slf4j;
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.oroqen.entity.OroqenHeritageProject;
import org.jeecg.modules.oroqen.service.IOroqenHeritageProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 非遗项目
 * @Author: jeecg-boot
 * @Date: 2024-01-01
 * @Version: V1.0
 */
@Tag(name="非遗项目", description="非遗项目")
@RestController
@RequestMapping("/oroqen/heritageProject")
@Slf4j
public class OroqenHeritageProjectController extends JeecgController<OroqenHeritageProject, IOroqenHeritageProjectService> {
	@Autowired
	private IOroqenHeritageProjectService oroqenHeritageProjectService;
	
	/**
	 * 分页列表查询
	 *
	 * @param oroqenHeritageProject
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "非遗项目-分页列表查询")
	@Operation(summary="非遗项目-分页列表查询", description="非遗项目-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OroqenHeritageProject oroqenHeritageProject,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OroqenHeritageProject> queryWrapper = QueryGenerator.initQueryWrapper(oroqenHeritageProject, req.getParameterMap());
		queryWrapper.eq("status", 1); // 只查询启用状态的项目
		queryWrapper.orderByDesc("is_recommended").orderByAsc("sort_order").orderByDesc("create_time");
		Page<OroqenHeritageProject> page = new Page<OroqenHeritageProject>(pageNo, pageSize);
		IPage<OroqenHeritageProject> pageList = oroqenHeritageProjectService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param oroqenHeritageProject
	 * @return
	 */
	@AutoLog(value = "非遗项目-添加")
	@Operation(summary="非遗项目-添加", description="非遗项目-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OroqenHeritageProject oroqenHeritageProject) {
		oroqenHeritageProjectService.save(oroqenHeritageProject);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param oroqenHeritageProject
	 * @return
	 */
	@AutoLog(value = "非遗项目-编辑")
	@Operation(summary="非遗项目-编辑", description="非遗项目-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
	public Result<?> edit(@RequestBody OroqenHeritageProject oroqenHeritageProject) {
		oroqenHeritageProjectService.updateById(oroqenHeritageProject);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "非遗项目-通过id删除")
	@Operation(summary="非遗项目-通过id删除", description="非遗项目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		oroqenHeritageProjectService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "非遗项目-批量删除")
	@Operation(summary="非遗项目-批量删除", description="非遗项目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.oroqenHeritageProjectService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "非遗项目-通过id查询")
	@Operation(summary="非遗项目-通过id查询", description="非遗项目-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		OroqenHeritageProject oroqenHeritageProject = oroqenHeritageProjectService.getById(id);
		if(oroqenHeritageProject==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(oroqenHeritageProject);
	}

	/**
	 * 获取推荐项目列表
	 *
	 * @return
	 */
	@AutoLog(value = "非遗项目-获取推荐项目列表")
	@Operation(summary="非遗项目-获取推荐项目列表", description="非遗项目-获取推荐项目列表")
	@GetMapping(value = "/recommended")
	public Result<?> getRecommendedProjects() {
		QueryWrapper<OroqenHeritageProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", 1);
		queryWrapper.eq("is_recommended", 1);
		queryWrapper.orderByDesc("create_time");
		List<OroqenHeritageProject> list = oroqenHeritageProjectService.list(queryWrapper);
		return Result.OK(list);
	}

	/**
	 * 按级别获取项目列表
	 *
	 * @param level
	 * @return
	 */
	@AutoLog(value = "非遗项目-按级别获取项目列表")
	@Operation(summary="非遗项目-按级别获取项目列表", description="非遗项目-按级别获取项目列表")
	@GetMapping(value = "/byLevel")
	public Result<?> getProjectsByLevel(@RequestParam(name="level",required=true) String level) {
		QueryWrapper<OroqenHeritageProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", 1);
		queryWrapper.eq("heritage_level", level);
		queryWrapper.orderByDesc("create_time");
		List<OroqenHeritageProject> list = oroqenHeritageProjectService.list(queryWrapper);
		return Result.OK(list);
	}

	/**
	 * 按类别获取项目列表
	 *
	 * @param category
	 * @return
	 */
	@AutoLog(value = "非遗项目-按类别获取项目列表")
	@Operation(summary="非遗项目-按类别获取项目列表", description="非遗项目-按类别获取项目列表")
	@GetMapping(value = "/byCategory")
	public Result<?> getProjectsByCategory(@RequestParam(name="category",required=true) String category) {
		QueryWrapper<OroqenHeritageProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", 1);
		queryWrapper.eq("category", category);
		queryWrapper.orderByDesc("create_time");
		List<OroqenHeritageProject> list = oroqenHeritageProjectService.list(queryWrapper);
		return Result.OK(list);
	}

	/**
	 * 获取所有级别统计
	 *
	 * @return
	 */
	@AutoLog(value = "非遗项目-获取所有级别统计")
	@Operation(summary="非遗项目-获取所有级别统计", description="非遗项目-获取所有级别统计")
	@GetMapping(value = "/levelStats")
	public Result<List<Map<String, Object>>> getLevelStats() {
		try {
			// 手动构建统计数据
			List<Map<String, Object>> stats = new ArrayList<>();
			
			// 查询国家级项目数量
			QueryWrapper<OroqenHeritageProject> nationalQuery = new QueryWrapper<>();
			nationalQuery.eq("status", 1).eq("heritage_level", "国家级");
			long nationalCount = oroqenHeritageProjectService.count(nationalQuery);
			
			Map<String, Object> nationalStat = new HashMap<>();
			nationalStat.put("heritage_level", "国家级");
			nationalStat.put("heritageLevel", "国家级");
			nationalStat.put("count", nationalCount);
			stats.add(nationalStat);
			
			// 查询省级项目数量
			QueryWrapper<OroqenHeritageProject> provincialQuery = new QueryWrapper<>();
			provincialQuery.eq("status", 1).eq("heritage_level", "省级");
			long provincialCount = oroqenHeritageProjectService.count(provincialQuery);
			
			Map<String, Object> provincialStat = new HashMap<>();
			provincialStat.put("heritage_level", "省级");
			provincialStat.put("heritageLevel", "省级");
			provincialStat.put("count", provincialCount);
			stats.add(provincialStat);
			
			// 查询市级项目数量
			QueryWrapper<OroqenHeritageProject> municipalQuery = new QueryWrapper<>();
			municipalQuery.eq("status", 1).eq("heritage_level", "市级");
			long municipalCount = oroqenHeritageProjectService.count(municipalQuery);
			
			Map<String, Object> municipalStat = new HashMap<>();
			municipalStat.put("heritage_level", "市级");
			municipalStat.put("heritageLevel", "市级");
			municipalStat.put("count", municipalCount);
			stats.add(municipalStat);
			
			return Result.OK(stats);
		} catch (Exception e) {
			log.error("获取级别统计失败", e);
			return Result.error("获取级别统计失败: " + e.getMessage());
		}
	}

    /**
     * 导出excel
     *
     * @param request
     * @param oroqenHeritageProject
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenHeritageProject oroqenHeritageProject) {
        return super.exportXls(request, oroqenHeritageProject, OroqenHeritageProject.class, "非遗项目");
    }

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

    /**
     * 推荐/取消推荐
     *
     * @param id
     * @param isRecommended
     * @return
     */
    @AutoLog(value = "非遗项目-推荐/取消推荐")
    @Operation(summary="非遗项目-推荐/取消推荐", description="非遗项目-推荐/取消推荐")
    @PutMapping(value = "/recommend")
    public Result<?> recommend(@RequestParam(name="id",required=true) String id, 
                              @RequestParam(name="isRecommended",required=true) Integer isRecommended) {
        try {
            OroqenHeritageProject project = oroqenHeritageProjectService.getById(id);
            if (project == null) {
                return Result.error("项目不存在");
            }
            project.setIsRecommended(isRecommended);
            oroqenHeritageProjectService.updateById(project);
            String action = isRecommended == 1 ? "推荐" : "取消推荐";
            return Result.OK(action + "成功！");
        } catch (Exception e) {
            log.error("推荐操作失败", e);
            return Result.error("推荐操作失败: " + e.getMessage());
        }
    }

}