package org.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.stm.audit.entity.AuditCheckMethodItem;
import org.stm.audit.entity.AuditComplianeItem;
import org.stm.audit.entity.AuditItemRelCheck;
import org.stm.audit.service.IAuditActRelOthersService;
import org.stm.audit.service.IAuditCheckMethodItemService;
import org.stm.audit.service.IAuditItemRelCheckService;

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

/**
 * @Description: 合规条目与检查方法关系
 * @Author: jeecg-boot
 * @Date:   2024-09-25
 * @Version: V1.0
 */
@Api(tags="合规条目与检查方法关系")
@RestController
@RequestMapping("/audit/auditItemRelCheck")
@Slf4j
public class AuditItemRelCheckController extends JeecgController<AuditItemRelCheck, IAuditItemRelCheckService> {
	@Autowired
	private IAuditItemRelCheckService auditItemRelCheckService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	@Autowired
	private IAuditCheckMethodItemService auditCheckMethodItemService;
	/**
	 * 分页列表查询
	 *
	 * @param auditItemRelCheck
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "合规条目与检查方法关系-分页列表查询")
	@ApiOperation(value="合规条目与检查方法关系-分页列表查询", notes="合规条目与检查方法关系-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AuditItemRelCheck>> queryPageList(AuditItemRelCheck auditItemRelCheck,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditItemRelCheck> queryWrapper = QueryGenerator.initQueryWrapper(auditItemRelCheck, req.getParameterMap());
		Page<AuditItemRelCheck> page = new Page<AuditItemRelCheck>(pageNo, pageSize);
		IPage<AuditItemRelCheck> pageList = auditItemRelCheckService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param auditItemRelCheck
	 * @return
	 */
	@AutoLog(value = "合规条目与检查方法关系-添加")
	@ApiOperation(value="合规条目与检查方法关系-添加", notes="合规条目与检查方法关系-添加")
	@RequiresPermissions("audit:audit_item_rel_check:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditItemRelCheck auditItemRelCheck) {
		auditItemRelCheckService.save(auditItemRelCheck);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param auditItemRelCheck
	 * @return
	 */
	@AutoLog(value = "合规条目与检查方法关系-编辑")
	@ApiOperation(value="合规条目与检查方法关系-编辑", notes="合规条目与检查方法关系-编辑")
	@RequiresPermissions("audit:audit_item_rel_check:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditItemRelCheck auditItemRelCheck) {
		auditItemRelCheckService.updateById(auditItemRelCheck);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "合规条目与检查方法关系-通过id删除")
	@ApiOperation(value="合规条目与检查方法关系-通过id删除", notes="合规条目与检查方法关系-通过id删除")
	@RequiresPermissions("audit:audit_item_rel_check:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditItemRelCheckService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "合规条目与检查方法关系-批量删除")
	@ApiOperation(value="合规条目与检查方法关系-批量删除", notes="合规条目与检查方法关系-批量删除")
	@RequiresPermissions("audit:audit_item_rel_check:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditItemRelCheckService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "合规条目与检查方法关系-通过id查询")
	@ApiOperation(value="合规条目与检查方法关系-通过id查询", notes="合规条目与检查方法关系-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AuditItemRelCheck> queryById(@RequestParam(name="id",required=true) String id) {
		AuditItemRelCheck auditItemRelCheck = auditItemRelCheckService.getById(id);
		if(auditItemRelCheck==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditItemRelCheck);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditItemRelCheck
    */
    @RequiresPermissions("audit:audit_item_rel_check:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditItemRelCheck auditItemRelCheck) {
        return super.exportXls(request, auditItemRelCheck, AuditItemRelCheck.class, "合规条目与检查方法关系");
    }

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

	 @GetMapping(value = "/queryItemRelCheckMethod")
	 public Result<Map<String,List<Map<String,Object>>>> queryItemRelCheckMethod(AuditComplianeItem auditComplianeItem, HttpServletRequest req) {
		List<Map<String,Object>> dataList = auditItemRelCheckService.queryCompItemCheckMethods(auditComplianeItem);
		 Map<String,List<Map<String,Object>>> retData = new HashMap<>();
		 List<Integer> checkIdList = new ArrayList<>();
		 for(Map<String,Object> tmpMap : dataList) {
			 String citemid = tmpMap.get("citemid").toString();
			 Integer checkid = Integer.valueOf(tmpMap.get("checkid").toString());
			 checkIdList.add(checkid);
			 List<Map<String,Object>> subList = retData.get(citemid);
			 if(subList==null) {
				 subList = new ArrayList<>();
				 retData.put(citemid,subList);
			 }
			 subList.add(tmpMap);
		 }
		 setActNames(checkIdList,dataList);
		 setCheckMethodItems(checkIdList,dataList);
		 return Result.OK(retData);
	 }


	@GetMapping(value = "/queryItemCheckMethodByItemId")
	public Result<List<Map<String,Object>>> queryItemCheckMethodByItemId(AuditComplianeItem auditComplianeItem, HttpServletRequest req) {
		List<Map<String,Object>> dataList = auditItemRelCheckService.queryCompItemCheckMethods(auditComplianeItem);
		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,5);
			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<AuditCheckMethodItem> queryWrapper = new QueryWrapper<>();
			queryWrapper.in("checkid",checkIdList);
			List<AuditCheckMethodItem> methodItems = auditCheckMethodItemService.list(queryWrapper);
			Map<Integer,List<AuditCheckMethodItem>> itemMap = new HashMap<>();
			for(AuditCheckMethodItem item:methodItems) {
				List<AuditCheckMethodItem>  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));
			}
		}
	}


}
