package org.jeecg.modules.stm.audit.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 lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.audit.entity.AuditFileContentCategories;
import org.jeecg.modules.stm.audit.service.IAuditFileContentCategoriesService;

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

 /**
 * @Description: 条目内容分类
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditFileContentCategories")
@Slf4j
public class AuditFileContentCategoriesController extends JeecgController<AuditFileContentCategories, IAuditFileContentCategoriesService> {
	@Autowired
	private IAuditFileContentCategoriesService auditFileContentCategoriesService;
	
	/**
	 * 分页列表查询
	 *
	 * @param auditFileContentCategories
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "条目内容分类-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditFileContentCategories>> queryPageList(AuditFileContentCategories auditFileContentCategories,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditFileContentCategories> queryWrapper = QueryGenerator.initQueryWrapper(auditFileContentCategories, req.getParameterMap());
		Page<AuditFileContentCategories> page = new Page<AuditFileContentCategories>(pageNo, pageSize);
		IPage<AuditFileContentCategories> pageList = auditFileContentCategoriesService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/alllist")
	 public Result<List<AuditFileContentCategories>> queryAllList(
			 AuditFileContentCategories auditFileContentCategories, HttpServletRequest req) {
		 QueryWrapper<AuditFileContentCategories> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("file_id", auditFileContentCategories.getFileId());
		 List<AuditFileContentCategories> list = auditFileContentCategoriesService.list(queryWrapper);
		 return Result.OK(list);
	 }

	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditFileContentCategories auditFileContentCategories) {
		auditFileContentCategories.setHasChild(false);
		auditFileContentCategoriesService.save(auditFileContentCategories);
		Integer pid = auditFileContentCategories.getParentId();
		if(pid!=null) {
			AuditFileContentCategories aud = auditFileContentCategoriesService.getById(pid);
			if(aud.getHasChild()==null||!aud.getHasChild()){
				aud.setHasChild(true);
				auditFileContentCategoriesService.saveOrUpdate(aud);
			}
		}

		return Result.OK("添加成功！");
	}

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditFileContentCategories auditFileContentCategories) {
		auditFileContentCategoriesService.updateById(auditFileContentCategories);
		return Result.OK("编辑成功!");
	}

	 @PostMapping(value = "/saveOrUpdate")
	 public Result<String> saveOrUpdate(@RequestBody List<AuditFileContentCategories> clist) {
		Map<Integer,AuditFileContentCategories> parentMap  =  new HashMap();
		List<Integer> pIdList = new ArrayList<>();
		for(AuditFileContentCategories cat:clist) {
			if(cat.getParentId()==null||cat.getParentId().equals("")){
				cat.setHasChild(false);
			} else {
				cat.setHasChild(false);
				pIdList.add(cat.getParentId());
			}
		}
		auditFileContentCategoriesService.saveOrUpdateBatch(clist);
		if(pIdList.size()>0) {
			List<AuditFileContentCategories> parentList = auditFileContentCategoriesService.listByIds(pIdList);
			for (AuditFileContentCategories cat : parentList) {
				cat.setHasChild(true);
			}
			auditFileContentCategoriesService.saveOrUpdateBatch(parentList);
		}
		return Result.OK("保存成功！");
	 }

	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditFileContentCategoriesService.removeById(id);
		return Result.OK("删除成功!");
	}

	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditFileContentCategoriesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("删除成功!");
	}

	@GetMapping(value = "/queryById")
	public Result<AuditFileContentCategories> queryById(@RequestParam(name="id",required=true) String id) {
		AuditFileContentCategories auditFileContentCategories = auditFileContentCategoriesService.getById(id);
		if(auditFileContentCategories==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditFileContentCategories);
	}

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditFileContentCategories auditFileContentCategories) {
        return super.exportXls(request, auditFileContentCategories, AuditFileContentCategories.class, "条目内容分类");
    }


    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AuditFileContentCategories.class);
    }

	 @GetMapping(value = "/queryNameById")
	 public Result<AuditFileContentCategories> queryTreePathById(
			 @RequestParam(name="fileId",required=true) Integer fileId,
			 @RequestParam(name="id",required=true) String id) {
		 AuditFileContentCategories auditFileContentCategories = auditFileContentCategoriesService.getById(id);
		 auditFileContentCategoriesService.queryAllDataByFileId(fileId);
		 if(auditFileContentCategories==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(auditFileContentCategories);
	 }

}
