package org.jeecg.modules.stm.audit.controller;

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

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.audit.entity.AuditInnerCheckmethodItem;
import org.jeecg.modules.stm.audit.entity.AuditInnerOrder;
import org.jeecg.modules.stm.audit.entity.AuditInnerOrderRelCheck;

import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.audit.service.IAuditCheckMethodItemService;
import org.jeecg.modules.stm.audit.service.IAuditInnerCheckmethodItemService;
import org.jeecg.modules.stm.audit.service.IAuditInnerOrderRelCheckService;

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.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
 
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.modules.stm.utils.StmConstans;

/**
 * @Description: 内控制度与检查点对应关系
 * @Author: jeecg-boot
 * @Date:   2025-03-19
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditInnerOrderRelCheck")
@Slf4j
public class AuditInnerOrderRelCheckController extends JeecgController<AuditInnerOrderRelCheck, IAuditInnerOrderRelCheckService> {
	@Autowired
	private IAuditInnerOrderRelCheckService auditInnerOrderRelCheckService;
	 @Autowired
	 private IAuditActRelOthersService stActRelOthersService;
	 @Autowired
	 private IAuditCheckMethodItemService auditCheckMethodItemService;
	@Autowired
	private IAuditInnerCheckmethodItemService auditInnerCheckmethodItemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param auditInnerOrderRelCheck
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "内控制度与检查点对应关系-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditInnerOrderRelCheck>> queryPageList(AuditInnerOrderRelCheck auditInnerOrderRelCheck,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditInnerOrderRelCheck> queryWrapper = QueryGenerator.initQueryWrapper(auditInnerOrderRelCheck, req.getParameterMap());
		Page<AuditInnerOrderRelCheck> page = new Page<AuditInnerOrderRelCheck>(pageNo, pageSize);
		IPage<AuditInnerOrderRelCheck> pageList = auditInnerOrderRelCheckService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param auditInnerOrderRelCheck
	 * @return
	 */
	@AutoLog(value = "内控制度与检查点对应关系-添加")
	  
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditInnerOrderRelCheck auditInnerOrderRelCheck) {
		auditInnerOrderRelCheckService.save(auditInnerOrderRelCheck);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param auditInnerOrderRelCheck
	 * @return
	 */
	@AutoLog(value = "内控制度与检查点对应关系-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditInnerOrderRelCheck auditInnerOrderRelCheck) {
		auditInnerOrderRelCheckService.updateById(auditInnerOrderRelCheck);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "内控制度与检查点对应关系-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditInnerOrderRelCheckService.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.auditInnerOrderRelCheckService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "内控制度与检查点对应关系-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditInnerOrderRelCheck> queryById(@RequestParam(name="id",required=true) String id) {
		AuditInnerOrderRelCheck auditInnerOrderRelCheck = auditInnerOrderRelCheckService.getById(id);
		if(auditInnerOrderRelCheck==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditInnerOrderRelCheck);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditInnerOrderRelCheck
    */
    @RequiresPermissions("audit:audit_inner_order_rel_check:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditInnerOrderRelCheck auditInnerOrderRelCheck) {
        return super.exportXls(request, auditInnerOrderRelCheck, AuditInnerOrderRelCheck.class, "内控制度与检查点对应关系");
    }

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

	 @GetMapping(value = "/queryItemCheckMethodByItemId")
	 public Result<List<Map<String,Object>>> queryItemCheckMethodByItemId(AuditInnerOrder auditInnerOrder, HttpServletRequest req) {
		 List<Map<String,Object>> dataList = auditInnerOrderRelCheckService.queryInnerOrderCheckmethod(auditInnerOrder);
		 List<Integer> checkIdList = new ArrayList<>();
		 for(Map<String,Object> tmpMap : dataList) {
			 Integer checkid = Integer.valueOf(tmpMap.get("checkid").toString());
			 checkIdList.add(checkid);
		 }
		 setActNames(checkIdList,dataList);
		 setCheckMethodItems(checkIdList,dataList);
		 return Result.OK(dataList);
	 }


	 private void setActNames(List<Integer> checkIdList, List<Map<String,Object>> dataList) {
		 if (checkIdList.size() > 0) {
			 Map<Integer, String> idActMap = stActRelOthersService.queryAllActByTids(checkIdList, StmConstans.Audit_TP_INNER_ORDER_CHECK_METHOD);
			 for (Map<String, Object> tmap : dataList) {
				 Integer tid = new Integer(tmap.get("checkid").toString());
				 String tempStr = idActMap.get(tid);
				 if(tempStr!=null) {
					 int p = tempStr.indexOf("|");
					 tmap.put("caName", tempStr.substring(0,p));
					 tmap.put("actIds", tempStr.substring(p+1));
				 }
			 }
		 }
	 }

	 private void setCheckMethodItems(List<Integer> checkIdList, List<Map<String,Object>> dataList) {
		 if (checkIdList.size() > 0) {

			 QueryWrapper<AuditInnerCheckmethodItem> queryWrapper = new QueryWrapper<>();
			 queryWrapper.in("checkid",checkIdList);
			 List<AuditInnerCheckmethodItem> methodItems = auditInnerCheckmethodItemService.list(queryWrapper);
			 Map<Integer,List<AuditInnerCheckmethodItem>> itemMap = new HashMap<>();
			 for(AuditInnerCheckmethodItem item:methodItems) {
				 List<AuditInnerCheckmethodItem>  tmpList = itemMap.get(item.getCheckid());
				 if(tmpList==null){
					 tmpList = new ArrayList<>();
				 }
				 tmpList.add(item);
				 itemMap.put(item.getCheckid(),tmpList);
			 }

			 for (Map<String, Object> tmap : dataList) {
				 Integer tid = new Integer(tmap.get("checkid").toString());
				 tmap.put("checkMethodItems",itemMap.get(tid));
			 }
		 }
	 }
 }
