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.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.AuditActRelOthers;
import org.jeecg.modules.stm.audit.entity.AuditCheckMethod;
import org.jeecg.modules.stm.audit.entity.AuditCheckMethodItem;
import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.audit.service.IAuditCheckMethodItemService;
import org.jeecg.modules.stm.audit.service.IAuditCheckMethodService;
import org.jeecg.modules.stm.utils.StmConstans;

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

/**
 * @Description: 检查方法
 * @Author: jeecg-boot
 * @Date:   2024-08-26
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditCheckMethod")
@Slf4j
public class AuditCheckMethodController extends JeecgController<AuditCheckMethod, IAuditCheckMethodService> {
	@Autowired
	private IAuditCheckMethodService auditCheckMethodService;
	@Autowired
	private IAuditLabelRelLibService stLabelRelLibService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	@Autowired
	private IAuditCheckMethodItemService auditCheckMethodItemService;
	/**
	 * 分页列表查询
	 *
	 * @param auditCheckMethod
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "检查方法-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditCheckMethod>> queryPageList(AuditCheckMethod auditCheckMethod,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditCheckMethod> queryWrapper = QueryGenerator.initQueryWrapper(auditCheckMethod, req.getParameterMap());
		Page<AuditCheckMethod> page = new Page<AuditCheckMethod>(pageNo, pageSize);
		IPage<AuditCheckMethod> pageList = auditCheckMethodService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	@GetMapping(value = "/queryAllChecks")
	public Result<List<AuditCheckMethod>> queryAllChecks(AuditCheckMethod auditCheckMethod,
														 HttpServletRequest req) {
		QueryWrapper<AuditCheckMethod> queryWrapper = QueryGenerator.initQueryWrapper(auditCheckMethod, req.getParameterMap());
		List<AuditCheckMethod> retList = auditCheckMethodService.list(queryWrapper);
		return Result.OK(retList);
	}


	@GetMapping(value = "/findAllChecks")
	public Result<List<Map<String,Object>>> findAllChecks(AuditCheckMethod auditCheckMethod) {
		List<Map<String,Object>> list = auditCheckMethodService.getAllCheckMethod(auditCheckMethod);
		return Result.OK(list);
	}

	/**
	 *   添加
	 *
	 * @param auditCheckMethod
	 * @return
	 */
	@AutoLog(value = "检查方法-添加")
	  
	@PostMapping(value = "/add")
	public Result<AuditCheckMethod> add(@RequestBody AuditCheckMethod auditCheckMethod) {
		auditCheckMethod.setIsPublic(true);
		auditCheckMethodService.save(auditCheckMethod);

		String labelCodes = auditCheckMethod.getLabels();
		Integer tid =  auditCheckMethod.getId();
		if(labelCodes!=null&&!labelCodes.equals("")) {
			stLabelRelLibService.saveLabelRel(labelCodes, tid, StmConstans.Audit_TP_CHECK_METHOD);
		}
		String actIds = auditCheckMethod.getActIds();
		if(actIds!=null&&!actIds.equals("")) {
			List<AuditActRelOthers> actRelOthers = new ArrayList<>();
			stActRelOthersService.setActRelations(actRelOthers,actIds,tid,StmConstans.Audit_TP_CHECK_METHOD);
			stActRelOthersService.saveOrUpdateBatch(actRelOthers);
		}

		List<AuditCheckMethodItem> cmItems = auditCheckMethod.getCheckMethodItems();
		if(cmItems!=null&&cmItems.size()>0) {
			for(AuditCheckMethodItem item:cmItems) {
				item.setCheckid(auditCheckMethod.getId());
			}
			auditCheckMethodItemService.saveOrUpdateBatch(cmItems);
		}
		return Result.OK(auditCheckMethod);
	}
	
	/**
	 *  编辑
	 *
	 * @param auditCheckMethod
	 * @return
	 */
	@AutoLog(value = "检查方法-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<AuditCheckMethod> edit(@RequestBody AuditCheckMethod auditCheckMethod) {
		auditCheckMethod.setIsPublic(true);
		auditCheckMethodService.updateById(auditCheckMethod);
		Integer tid =  auditCheckMethod.getId();
		String labels = auditCheckMethod.getLabels();
		if(labels!=null) {
			stLabelRelLibService.deleteLabelRel(tid,StmConstans.Audit_TP_CHECK_METHOD);
			if(labels.trim().length()>0){
				stLabelRelLibService.saveLabelRel(labels,tid, StmConstans.Audit_TP_CHECK_METHOD);
			}
		}
		String actIds = auditCheckMethod.getActIds();
		if(actIds!=null) {
			List<AuditActRelOthers> actRelOthers = new ArrayList<>();
			stActRelOthersService.deleteActRel(tid.toString(), StmConstans.Audit_TP_CHECK_METHOD);
			if (actIds.trim().length() > 0) {
				stActRelOthersService.setActRelations(actRelOthers,actIds,tid,StmConstans.Audit_TP_CHECK_METHOD);
				stActRelOthersService.saveOrUpdateBatch(actRelOthers);
			}
		}
		List<AuditCheckMethodItem> cmItems = auditCheckMethod.getCheckMethodItems();
		if(cmItems!=null&&cmItems.size()>0) {
			auditCheckMethodItemService.saveOrUpdateBatch(cmItems);
		}
		return Result.OK(auditCheckMethod);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "检查方法-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditCheckMethodService.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.auditCheckMethodService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "检查方法-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditCheckMethod> queryById(@RequestParam(name="id",required=true) String id) {
		AuditCheckMethod auditCheckMethod = auditCheckMethodService.getById(id);
		if(auditCheckMethod==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditCheckMethod);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditCheckMethod
    */
    //@RequiresPermissions("audit:audit_check_method:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditCheckMethod auditCheckMethod) {
        return super.exportXls(request, auditCheckMethod, AuditCheckMethod.class, "检查方法");
    }

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


}
