package org.jeecg.modules.stm.wb.manage.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.wb.manage.entity.WbCompanyThirdApi;
import org.jeecg.modules.stm.wb.manage.entity.WbCompanyThirdApiVo;
import org.jeecg.modules.stm.wb.manage.entity.WbPullDataLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.wb.manage.service.IWbCompanyThirdApiService;
import org.jeecg.modules.stm.wb.manage.service.IWbPullDataLogService;

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

/**
 * @Description: wb_company_third_api
 * @Author: jeecg-boot
 * @Date:   2024-03-09
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/wb.manage/wbCompanyThirdApi")
@Slf4j
public class WbCompanyThirdApiController extends JeecgController<WbCompanyThirdApi, IWbCompanyThirdApiService> {
	@Autowired
	private IWbCompanyThirdApiService wbCompanyThirdApiService;
	 @Autowired
	 private IWbPullDataLogService wbPullDataLogService;
	/**
	 * 分页列表查询
	 *
	 * @param wbCompanyThirdApi
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "wb_company_third_api-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<WbCompanyThirdApi>> queryPageList(WbCompanyThirdApi wbCompanyThirdApi,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WbCompanyThirdApi> queryWrapper = QueryGenerator.initQueryWrapper(wbCompanyThirdApi, req.getParameterMap());
		Page<WbCompanyThirdApi> page = new Page<WbCompanyThirdApi>(pageNo, pageSize);
		IPage<WbCompanyThirdApi> pageList = wbCompanyThirdApiService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param wbCompanyThirdApi
	 * @return
	 */
	@AutoLog(value = "wb_company_third_api-添加")
	  
	//@RequiresPermissions("wb.manage:wb_company_third_api:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WbCompanyThirdApi wbCompanyThirdApi) {
		wbCompanyThirdApiService.save(wbCompanyThirdApi);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param wbCompanyThirdApi
	 * @return
	 */
	@AutoLog(value = "wb_company_third_api-编辑")
	  
	//@RequiresPermissions("wb.manage:wb_company_third_api:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WbCompanyThirdApi wbCompanyThirdApi) {
		wbCompanyThirdApiService.updateById(wbCompanyThirdApi);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "wb_company_third_api-通过id删除")
	  
	//@RequiresPermissions("wb.manage:wb_company_third_api:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		wbCompanyThirdApiService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "wb_company_third_api-批量删除")
	  
	//@RequiresPermissions("wb.manage:wb_company_third_api:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wbCompanyThirdApiService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "wb_company_third_api-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<WbCompanyThirdApi> queryById(@RequestParam(name="id",required=true) String id) {
		WbCompanyThirdApi wbCompanyThirdApi = wbCompanyThirdApiService.getById(id);
		if(wbCompanyThirdApi==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(wbCompanyThirdApi);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param wbCompanyThirdApi
    */
    @RequiresPermissions("wb.manage:wb_company_third_api:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WbCompanyThirdApi wbCompanyThirdApi) {
        return super.exportXls(request, wbCompanyThirdApi, WbCompanyThirdApi.class, "wb_company_third_api");
    }

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


	   
	 @GetMapping(value = "/queryThirdApi")
	 public Result<IPage<WbCompanyThirdApi>> queryThirdApi(WbCompanyThirdApi wbCompanyThirdApi,
						   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
					       @RequestParam(name="cid") Integer cid,
						   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
						   HttpServletRequest req) {
		 QueryWrapper<WbCompanyThirdApi> queryWrapper = QueryGenerator.initQueryWrapper(wbCompanyThirdApi, req.getParameterMap());
		 Page<WbCompanyThirdApi> page = new Page<WbCompanyThirdApi>(pageNo, pageSize);
		 IPage<WbCompanyThirdApi> pageList = wbCompanyThirdApiService.page(page, queryWrapper);

		 List<Integer> apiIdList  = new ArrayList<>();
		 for(WbCompanyThirdApi wbApi : pageList.getRecords()) {
			 apiIdList.add(wbApi.getApiId());
		 }
		 Map<Integer, WbPullDataLog> tmpMap = new HashMap<>();
		 List<WbPullDataLog> listLog = wbPullDataLogService.findLog(cid,apiIdList);
		 for(WbPullDataLog wbLog : listLog) {
			 tmpMap.put(wbLog.getApiId(),wbLog);
		 }
		 for(WbCompanyThirdApi wbApi : pageList.getRecords()) {
			 WbPullDataLog wlog = tmpMap.get(wbApi.getApiId());
			 if(wlog==null) {
				 //wbApi.setPulled(-1);
				 //wbApi.setStaMsg("未抓取");
			 } else {
				 wbApi.setPulled(wlog.getSta());
				 wbApi.setStaMsg(wlog.getStaMsg());
			 }
		 }
		 return Result.OK(pageList);
	 }


	@RequestMapping(value = "/setRiskLevel", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> setRiskLevel(@RequestBody WbCompanyThirdApiVo wbCompanyThirdApiVo) {
		List<Integer> tidList = wbCompanyThirdApiVo.getTids();
		if(tidList==null&&tidList.isEmpty()) {
			return Result.OK("缺少数据，保存失败！");
		}

		List<WbCompanyThirdApi> dataList = new ArrayList<WbCompanyThirdApi>();
		for(Integer id:tidList) {
			if(wbCompanyThirdApiVo.getRiskLevel()!=null) {
				WbCompanyThirdApi wbCheckCfg= new WbCompanyThirdApi();
				wbCheckCfg.setId(id);
				wbCheckCfg.setRiskLevel(wbCompanyThirdApiVo.getRiskLevel());
				dataList.add(wbCheckCfg);
			}
		}
		wbCompanyThirdApiService.updateBatchById(dataList);
		return Result.OK("保存成功!");
	}


	@GetMapping(value = "/queryByRiskLevel")
	public Result<List<String>> queryByRiskLevel(WbCompanyThirdApi wbCompanyThirdApi, HttpServletRequest req) {
		QueryWrapper<WbCompanyThirdApi> queryWrapper = QueryGenerator.initQueryWrapper(wbCompanyThirdApi, req.getParameterMap());
		List <WbCompanyThirdApi> list = wbCompanyThirdApiService.list(queryWrapper);
		List<String> retList = new ArrayList<>();
		for(WbCompanyThirdApi api:list) {
			retList.add(api.getId().toString());
		}
		return Result.OK(retList);
	}
 }
