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.stm.audit.service.IAuditLabelRelLibService;
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.AuditBestPractice;
import org.jeecg.modules.stm.audit.service.IAuditBestPracticeService;
import org.jeecg.modules.stm.utils.StmConstans;

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

/**
 * @Description: 行业实践
 * @Author: jeecg-boot
 * @Date:   2024-08-26
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditBestPractice")
@Slf4j
public class AuditBestPracticeController extends JeecgController<AuditBestPractice, IAuditBestPracticeService> {
	@Autowired
	private IAuditBestPracticeService auditBestPracticeService;
	@Autowired
	private IAuditLabelRelLibService stLabelRelLibService;
	/**
	 * 分页列表查询
	 *
	 * @param auditBestPractice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "行业实践-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditBestPractice>> queryPageList(AuditBestPractice auditBestPractice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditBestPractice> queryWrapper = QueryGenerator.initQueryWrapper(auditBestPractice, req.getParameterMap());
		Page<AuditBestPractice> page = new Page<AuditBestPractice>(pageNo, pageSize);
		IPage<AuditBestPractice> pageList = auditBestPracticeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/listAll")
	public Result<List<AuditBestPractice>> listAll(AuditBestPractice auditBestPractice,
														  HttpServletRequest req) {
		QueryWrapper<AuditBestPractice> queryWrapper = QueryGenerator.initQueryWrapper(auditBestPractice, req.getParameterMap());
		List<AuditBestPractice> list = auditBestPracticeService.list(queryWrapper);
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param auditBestPractice
	 * @return
	 */
	@AutoLog(value = "行业实践-添加")
	  
	@PostMapping(value = "/add")
	public Result<AuditBestPractice> add(@RequestBody AuditBestPractice auditBestPractice) {
		auditBestPracticeService.save(auditBestPractice);
		String labels = auditBestPractice.getLabels();
		if(labels!=null&&labels.length()>0){
			Integer tid =  auditBestPractice.getId();
			stLabelRelLibService.saveLabelRel(labels,tid, StmConstans.Audit_TP_BEST_PRACTICE);
		}
		return Result.OK(auditBestPractice);
	}
	
	/**
	 *  编辑
	 *
	 * @param auditBestPractice
	 * @return
	 */
	@AutoLog(value = "行业实践-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditBestPractice auditBestPractice) {
		auditBestPracticeService.updateById(auditBestPractice);
		String labels = auditBestPractice.getLabels();
		Integer tid =  auditBestPractice.getId();
		stLabelRelLibService.deleteLabelRel(tid,StmConstans.Audit_TP_BEST_PRACTICE);
		if(labels!=null && !labels.equals("")) {
			stLabelRelLibService.saveLabelRel(labels, tid, StmConstans.Audit_TP_BEST_PRACTICE);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "行业实践-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditBestPracticeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "行业实践-批量删除")
	  
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditBestPracticeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "行业实践-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditBestPractice> queryById(@RequestParam(name="id",required=true) String id) {
		AuditBestPractice auditBestPractice = auditBestPracticeService.getById(id);
		if(auditBestPractice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditBestPractice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditBestPractice
    */
    @RequiresPermissions("audit:audit_best_practice:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditBestPractice auditBestPractice) {
        return super.exportXls(request, auditBestPractice, AuditBestPractice.class, "行业实践");
    }

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

}
