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.IAuditInnerCheckmethodItemService;
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.AuditCheckMethodItem;
import org.jeecg.modules.stm.audit.entity.AuditInnerCheckmethodItem;
import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.audit.service.IAuditCheckMethodItemService;
import org.jeecg.modules.stm.utils.CommonUtil;

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-10-10
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditCheckMethodItem/")
@Slf4j
public class AuditCheckMethodItemController extends JeecgController<AuditCheckMethodItem, IAuditCheckMethodItemService> {
	@Autowired
	private IAuditCheckMethodItemService auditCheckMethodItemService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	@Autowired
	private IAuditInnerCheckmethodItemService auditInnerCheckmethodItemService;
	/**
	 * 分页列表查询
	 *
	 * @param auditCheckMethodItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "检查方法条目-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditCheckMethodItem>> queryPageList(AuditCheckMethodItem auditCheckMethodItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditCheckMethodItem> queryWrapper = QueryGenerator.initQueryWrapper(auditCheckMethodItem, req.getParameterMap());
		Page<AuditCheckMethodItem> page = new Page<AuditCheckMethodItem>(pageNo, pageSize);
		IPage<AuditCheckMethodItem> pageList = auditCheckMethodItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/queryAllCheckItems")
	public Result<List<AuditCheckMethodItem>> queryAllCheckItems(AuditCheckMethodItem auditCheckMethodItem, HttpServletRequest req) {
		QueryWrapper<AuditCheckMethodItem> queryWrapper = QueryGenerator.initQueryWrapper(auditCheckMethodItem, req.getParameterMap());
		List<AuditCheckMethodItem> list = auditCheckMethodItemService.list(queryWrapper);
		return Result.OK(list);
	}

	@GetMapping(value = "/queryAllCheckMethodItems")
	public Result<List<AuditCheckMethodItem>> queryAllCheckMethodItems(AuditCheckMethodItem auditCheckMethodItem,HttpServletRequest req) {
		QueryWrapper<AuditCheckMethodItem> queryWrapper = QueryGenerator.initQueryWrapper(auditCheckMethodItem, req.getParameterMap());
		List<AuditCheckMethodItem> list = auditCheckMethodItemService.list(queryWrapper);
		return Result.OK(list);
	}

	/**
	 *   添加
	 *
	 * @param auditCheckMethodItem
	 * @return
	 */
	@AutoLog(value = "检查方法条目-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditCheckMethodItem auditCheckMethodItem) {
		auditCheckMethodItem.setName(CommonUtil.replaceSpecialChar(auditCheckMethodItem.getName()));
		auditCheckMethodItemService.save(auditCheckMethodItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param auditCheckMethodItem
	 * @return
	 */
	@AutoLog(value = "检查方法条目-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditCheckMethodItem auditCheckMethodItem) {
		auditCheckMethodItem.setName(CommonUtil.replaceSpecialChar(auditCheckMethodItem.getName()));
		auditCheckMethodItemService.updateById(auditCheckMethodItem);
		return Result.OK("编辑成功!");
	}

	@RequestMapping(value = "/editName", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editName(@RequestBody AuditCheckMethodItem auditCheckMethodItem,HttpServletRequest req) {
		String newName = auditCheckMethodItem.getName();
		AuditCheckMethodItem updateItem = new AuditCheckMethodItem();
		updateItem.setName(newName);
		AuditCheckMethodItem item = auditCheckMethodItemService.getById(auditCheckMethodItem.getId());
		String oldName = item.getName();
		QueryWrapper<AuditCheckMethodItem> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name",oldName);
		boolean isUpdated = auditCheckMethodItemService.update(updateItem, queryWrapper);
		if (isUpdated) {
			return Result.OK("修改成功!");
		} else {
			return Result.OK("修改失败!");
		}
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "检查方法条目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditCheckMethodItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "检查方法条目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public void deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditCheckMethodItemService.removeByIds(Arrays.asList(ids.split(",")));
		//return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "检查方法条目-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditCheckMethodItem> queryById(@RequestParam(name="id",required=true) String id) {
		AuditCheckMethodItem auditCheckMethodItem = auditCheckMethodItemService.getById(id);
		if(auditCheckMethodItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditCheckMethodItem);
	}

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

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

	 @GetMapping(value = "/queryByCheckId")
	 public Result<List<AuditCheckMethodItem>> queryByCheckId(AuditCheckMethodItem auditCheckMethodItem,
															  HttpServletRequest req) {
		 QueryWrapper<AuditCheckMethodItem> queryWrapper = new QueryWrapper<>();
		 Integer checkId = auditCheckMethodItem.getCheckid();
		 queryWrapper.eq("checkid",checkId);
		 List<AuditCheckMethodItem> retList = auditCheckMethodItemService.list(queryWrapper);
		 return Result.OK(retList);
	 }

	@GetMapping(value = "/getAllCheckMethodByItem")
	public Result<List<Map<String,Object>>> getAllCheckMethodByItem(AuditCheckMethodItem auditCheckMethodItem, HttpServletRequest req) {
		String comeFrom = req.getParameter("comeFrom");
		List<Map<String,Object>> list = new ArrayList<>();
		if(comeFrom==null||comeFrom.equals("2")) {
			list = auditCheckMethodItemService.getAllCheckMethodByItem(auditCheckMethodItem);
		} else {
			AuditInnerCheckmethodItem auditInnerCheckmethodItem = new AuditInnerCheckmethodItem();
			auditInnerCheckmethodItem.setName(auditCheckMethodItem.getName());
			list = auditInnerCheckmethodItemService.getAllInnerOrderCheckMethodByItem(auditInnerCheckmethodItem);
		}
		List<Integer> checkIdList = new ArrayList<>();
		for(Map<String,Object> data : list){
			checkIdList.add(Integer.valueOf(data.get("checkid").toString()));
		}
		if(list.size()>0){
			setActNames(checkIdList,list);
		}

		return Result.OK(list);
	}

	@GetMapping(value = "/getAllCheckMethod")
	public Result<List<Map<String,Object>>> getAllCheckMethod(AuditCheckMethodItem auditCheckMethodItem, HttpServletRequest req) {
		List<Map<String,Object>> list = auditCheckMethodItemService.getAllCheckMethod(auditCheckMethodItem);
		return Result.OK(list);
	}

	@GetMapping(value = "/getAllCheckMethodFiles")
	public Result<List<Map<String,Object>>> getAllCheckMethodFiles(AuditCheckMethodItem auditCheckMethodItem, HttpServletRequest req) {
		List<Map<String,Object>> list = auditCheckMethodItemService.getAllCheckMethodFiles(auditCheckMethodItem);
		return Result.OK(list);
	}

	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("caNames", tempStr.substring(0,p));
					tmap.put("actIds", tempStr.substring(p+1));
				}
			}
		}
	}

}
