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

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

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.standard.entity.StStandard;
import org.jeecg.modules.stm.standard.service.IStStandardService;

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.SpiderUtil;

/**
 * @Description: 行业标准
 * @Author: jeecg-boot
 * @Date:   2023-06-20
 * @Version: V1.0
 */

@RestController
@RequestMapping("/standard/stStandard")
@Slf4j
public class StStandardController extends JeecgController<StStandard, IStStandardService> {
	@Autowired
	private IStStandardService stStandardService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stStandard
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "行业标准-分页列表查询")

	@GetMapping(value = "/list")
	public Result<IPage<StStandard>> queryPageList(StStandard stStandard,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if(stStandard.getName()!=null) {
			stStandard.setName("*"+stStandard.getName()+ "*");
		}
		QueryWrapper<StStandard> queryWrapper = QueryGenerator.initQueryWrapper(stStandard, req.getParameterMap());
		Page<StStandard> page = new Page<StStandard>(pageNo, pageSize);
		IPage<StStandard> pageList = stStandardService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stStandard
	 * @return
	 */
	@AutoLog(value = "行业标准-添加")

	@RequiresPermissions("standard:st_standard:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StStandard stStandard) {
		stStandardService.save(stStandard);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stStandard
	 * @return
	 */
	@AutoLog(value = "行业标准-编辑")

	@RequiresPermissions("standard:st_standard:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StStandard stStandard) {
		stStandardService.updateById(stStandard);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "行业标准-通过id删除")

	@RequiresPermissions("standard:st_standard:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stStandardService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "行业标准-批量删除")

	@RequiresPermissions("standard:st_standard:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stStandardService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "行业标准-通过id查询")

	@GetMapping(value = "/queryById")
	public Result<StStandard> queryById(@RequestParam(name="id",required=true) String id) {
		StStandard stStandard = stStandardService.getById(id);
		if(stStandard==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stStandard);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stStandard
    */
    @RequiresPermissions("standard:st_standard:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StStandard stStandard) {
        return super.exportXls(request, stStandard, StStandard.class, "行业标准");
    }

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

	 /**
	  * 通过从官网上抓取数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequiresPermissions("standard:st_standard:importExcel")
	 @RequestMapping(value = "/spiderStandardData", method = RequestMethod.POST)
	 public Result<?> spiderStandardData(HttpServletRequest request, HttpServletResponse response) {
		 int total=0;
		 try {
			 JsonArray retAry = SpiderUtil.spiderStandardData();
			 total = retAry.size();
			 List<StStandard> listStStandard = new ArrayList<StStandard>();
			 for(int i=0;i<retAry.size();i++) {
				 JsonElement obj = retAry.get(i);
				 StStandard stStandard = new StStandard();
				 stStandard.setCreatedTime(new Date());
				 stStandard.setCreatedUser("admin");
				 stStandard.setUpdatedTime(new Date());
				 stStandard.setCreatedUser("admin");
				 stStandard.setId(i+1);
				 JsonObject jsonObject = obj.getAsJsonObject();
				 // 遍历 JsonObject 内的属性值
				 for (String key : jsonObject.keySet()) {
					 JsonElement value = jsonObject.get(key);
					 if(key.equals("actDate")){
						 stStandard.setEfftDate(new Date(value.getAsLong()));
					 } else  if(key.equals("chName")){
						 stStandard.setName(value.getAsString());
					 } else  if(key.equals("chargeDept")){
						 stStandard.setChargeDept(value.getAsString());
					 } else  if(key.equals("code")){
						 stStandard.setDocNo(value.getAsString());
					 } else  if(key.equals("industry")){
						 stStandard.setIndustry(value.getAsString());
					 } else  if(key.equals("issueDate")){
						 stStandard.setAppvDate(new Date(value.getAsLong()));
					 } else  if(key.equals("recordNo")){
						 stStandard.setFilingNum(value.getAsString());
					 } else  if(key.equals("recordDate")){
						 stStandard.setFillingDate(new Date(value.getAsLong()));
					 } else  if(key.equals("status")){
						 stStandard.setCurSta(value.getAsString());
					 } else  if(key.equals("pk")){
						 String strPath = "https://hbba.sacinfo.org.cn/stdDetail/"+value.getAsString();
						 stStandard.setLinkAddr(strPath);
					 }

				 }
				 listStStandard.add(stStandard);
			 }

			 stStandardService.remove(null);
			 stStandardService.saveBatch(listStStandard);

		 } catch (Exception e){
			 e.printStackTrace();
		 }
		 return Result.OK("共导入"+total+"条数据");
	 }

 }
