package org.jeecg.modules.stm.compliane.info.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.compliane.item.entity.StComplianeItem;
import org.jeecg.modules.stm.compliane.item.service.IStComplianeItemService;
import org.springframework.beans.factory.annotation.Value;
import org.jeecg.modules.stm.compliane.info.entity.StComplianeInfo;
import org.jeecg.modules.stm.compliane.info.service.IStComplianeInfoService;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.compliane.info.vo.StComplianeInfoVo;

import org.jeecg.modules.stm.utils.ParseDocUtil;
import java.util.*;

/**
 * @Description: 合规库
 * @Author: yzh
 * @Date:   2023-03-05
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/compliane.info/stComplianeInfo")
@Slf4j
public class StComplianeInfoController extends JeecgController<StComplianeInfo, IStComplianeInfoService> {
	@Autowired
	private IStComplianeInfoService stComplianeInfoService;
	@Autowired
	private IStComplianeItemService stComplianeItemService;

	@Value("${jeecg.path.upload}")
	private String uploadPath;
	
	/**
	 * 分页列表查询
	 *
	 * @param stComplianeInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "合规库-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StComplianeInfo>> queryPageList(StComplianeInfo stComplianeInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<StComplianeInfo> queryWrapper = QueryGenerator.initQueryWrapper(stComplianeInfo, req.getParameterMap());
//		Page<StComplianeInfo> page = new Page<StComplianeInfo>(pageNo, pageSize);
//		IPage<StComplianeInfo> pageList = stComplianeInfoService.page(page, queryWrapper);
		IPage<StComplianeInfo> pageList = stComplianeInfoService.defPage(stComplianeInfo, pageNo,pageSize,req.getParameterMap());
		return Result.OK(pageList);
	}

	   
	 @GetMapping(value = "/listAll")
	 public Result<IPage<StComplianeInfo>> queryPageListAll(StComplianeInfo stComplianeInfo,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest req) {
		 IPage<StComplianeInfo> pageList = stComplianeInfoService.defPageListAll(stComplianeInfo, pageNo,pageSize,req.getParameterMap());
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param stComplianeInfo
	 * @return
	 */
	@AutoLog(value = "合规库-添加")
	  
	@RequiresPermissions("compliane.info:st_compliane_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StComplianeInfoVo stComplianeInfo) {
		stComplianeInfoService.save(stComplianeInfo);
		Result<String> result = Result.OK("添加成功！");
		result.setResult(stComplianeInfo.getComplianeNo());
		//saveItems(stComplianeInfo);

		return result;
	}
	
	/**
	 *  编辑
	 *
	 * @param stComplianeInfo
	 * @return
	 */
	@AutoLog(value = "合规库-编辑")
	  
	@RequiresPermissions("compliane.info:st_compliane_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StComplianeInfoVo stComplianeInfo) {
		stComplianeInfoService.updateById(stComplianeInfo);
		Result<String> result = Result.OK("编辑成功！");
		result.setResult(stComplianeInfo.getComplianeNo());
		//saveItems(stComplianeInfo);
		return  result;
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "合规库-通过id删除")
	  
	@RequiresPermissions("compliane.info:st_compliane_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stComplianeInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "合规库-批量删除")
	  
	@RequiresPermissions("compliane.info:st_compliane_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stComplianeInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "合规库-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StComplianeInfo> queryById(@RequestParam(name="id",required=true) String id) {
		StComplianeInfo stComplianeInfo = stComplianeInfoService.getById(id);
		if(stComplianeInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stComplianeInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stComplianeInfo
    */
    @RequiresPermissions("compliane.info:st_compliane_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StComplianeInfo stComplianeInfo) {
        return super.exportXls(request, stComplianeInfo, StComplianeInfo.class, "合规库");
    }

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

	private void saveItems(StComplianeInfoVo stComplianeInfo) {
		Map<String,Object> delMap = new HashMap<String,Object>();
		delMap.put("COMPLIANE_NO",stComplianeInfo.getComplianeNo());
		stComplianeItemService.removeByMap(delMap);
		String strFilePath = stComplianeInfo.getFileUploadPath();
		if(strFilePath!=null&&strFilePath.trim().length()>0) {
			String fileTp = strFilePath.substring(strFilePath.lastIndexOf(".")+1);
			fileTp = fileTp.toLowerCase();
			strFilePath = uploadPath +"/" + strFilePath;
			List<String[]> listData = null;
			if (fileTp.equals("docx")) {
				listData =  ParseDocUtil.docxParser(strFilePath);
			} else if (fileTp.equals("doc")) {
				listData =  ParseDocUtil.docParser(strFilePath);
			} else {
				listData =  ParseDocUtil.pdfParserToList(strFilePath);
			}
			if(listData==null) {
				return;
			}
			listData = ParseDocUtil.parseForItem(listData);
			if(listData!=null&&listData.size()>0) {
				List<StComplianeItem> itemList = new ArrayList<StComplianeItem>();
				for (int i = 0; i < listData.size(); i++) {
					String[] data = listData.get(i);
					StComplianeItem stItem = new StComplianeItem();
					if (data[1] != null && !data[1].equals("")) {
						stItem.setComplianeNo(stComplianeInfo.getComplianeNo());
						stItem.setComplianeItem(data[0]);
						stItem.setComplianeAsk(data[1]);
						stItem.setCreatedTime(new Date());
						stItem.setCreatedUser("admin");
						stItem.setCaNo("ACT00104");
						stItem.setValid("1");
						stItem.setApplicableCode("0001");
						stItem.setApproveFlag("1");
						itemList.add(stItem);
					}
				}
				stComplianeItemService.saveBatch(itemList);
			}
		}
	}

}
