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.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.system.entity.SysCategory;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.jeecg.modules.stm.audit.entity.AuditProject;
import org.jeecg.modules.stm.audit.service.IAuditProjectService;
import org.jeecg.modules.stm.utils.UserUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目
 * @Author: jeecg-boot
 * @Date:   2025-01-07
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditProject")
@Slf4j
public class AuditProjectController extends JeecgController<AuditProject, IAuditProjectService> {
	@Autowired
	private IAuditProjectService auditProjectService;

	@Autowired
	private ISysCategoryService sysCategoryService;
	
	/**
	 * 分页列表查询
	 *
	 * @param auditProject
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "项目-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditProject>> queryPageList(AuditProject auditProject,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditProject> queryWrapper = QueryGenerator.initQueryWrapper(auditProject, req.getParameterMap());
		Page<AuditProject> page = new Page<AuditProject>(pageNo, pageSize);
		IPage<AuditProject> pageList = auditProjectService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 @GetMapping(value = "/listAll")
	 public Result<List<AuditProject>> listAll(AuditProject auditProject, HttpServletRequest req) {
		 QueryWrapper<AuditProject> queryWrapper = QueryGenerator.initQueryWrapper(auditProject, req.getParameterMap());
		 List<AuditProject> list = auditProjectService.list(queryWrapper);
		 return Result.OK(list);
	 }

	@GetMapping(value = "/getProjectChecks")
	public Result<List<Map<String,Object>>> getProjectChecks(AuditProject auditProject, HttpServletRequest req) {
		Integer pid = auditProject.getId();
		List<Map<String,Object>> list = auditProjectService.getProjectAllTask(null,pid);

		return Result.OK(list);
	}

	@GetMapping(value = "/getProjectChecksForUser")
	public Result<List<Map<String,Object>>> getProjectChecksForUser(AuditProject auditProject, HttpServletRequest req) {
		String userid = UserUtil.getUserId();
		Integer pid = auditProject.getId();
		List<Map<String,Object>> list = auditProjectService.getProjectAllTask(userid,pid);
		return Result.OK(list);
	}

	@AutoLog(value = "项目-添加")
	  
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditProject auditProject) {

		SysCategory sysCategory = sysCategoryService.getById(auditProject.getTp());
		auditProject.setTpName(sysCategory.getName());
		auditProjectService.save(auditProject);

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

	@AutoLog(value = "项目-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditProject auditProject) {
		SysCategory sysCategory = sysCategoryService.getById(auditProject.getTp());
		auditProject.setTpName(sysCategory.getName());
		auditProjectService.updateById(auditProject);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditProjectService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目-批量删除")
	  
	@RequiresPermissions("audit:audit_project:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> idList = Arrays.asList(ids.split(","));
		this.auditProjectService.removeByIds(idList);
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	  
	@GetMapping(value = "/queryById")
	public Result<AuditProject> queryById(@RequestParam(name="id",required=true) String id) {
		AuditProject auditProject = auditProjectService.getById(id);
		if(auditProject==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditProject);
	}





}
