package org.jeecg.modules.demo.ggzyqxqcodemapping.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.ggzyqxqcodemapping.entity.GgzyQxqCodeMapping;
import org.jeecg.modules.demo.ggzyqxqcodemapping.service.IGgzyQxqCodeMappingService;

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.modules.demo.ggzyregionalcodemapping.entity.GgzyRegionalCodeMapping;
import org.jeecg.modules.demo.ggzyregionalcodemapping.service.IGgzyRegionalCodeMappingService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 公共资源旗县区映射表
 * @Author: jeecg-boot
 * @Date:   2024-01-09
 * @Version: V1.0
 */
@Api(tags="公共资源旗县区映射表")
@RestController
@RequestMapping("/ggzyqxqcodemapping/ggzyQxqCodeMapping")
@Slf4j
public class GgzyQxqCodeMappingController extends JeecgController<GgzyQxqCodeMapping, IGgzyQxqCodeMappingService> {
	@Autowired
	private IGgzyQxqCodeMappingService ggzyQxqCodeMappingService;

	 @Autowired
	private IGgzyRegionalCodeMappingService iGgzyRegionalCodeMappingService;

	/**
	 * 分页列表查询
	 *
	 * @param ggzyQxqCodeMapping
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "公共资源旗县区映射表-分页列表查询")
	@ApiOperation(value="公共资源旗县区映射表-分页列表查询", notes="公共资源旗县区映射表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GgzyQxqCodeMapping>> queryPageList(GgzyQxqCodeMapping ggzyQxqCodeMapping,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GgzyQxqCodeMapping> queryWrapper = QueryGenerator.initQueryWrapper(ggzyQxqCodeMapping, req.getParameterMap());
		Page<GgzyQxqCodeMapping> page = new Page<GgzyQxqCodeMapping>(pageNo, pageSize);
		IPage<GgzyQxqCodeMapping> pageList = ggzyQxqCodeMappingService.page(page, queryWrapper);



		return Result.OK(pageList);
	}
	/*



	List<GgzyRegionalCodeMapping> list = iGgzyRegionalCodeMappingService.list();

		for (GgzyRegionalCodeMapping ggzyRegionalCodeMapping : list) {
			String code = ggzyRegionalCodeMapping.getCode();
			String Pcode = ggzyRegionalCodeMapping.getCode();
			String Pname = ggzyRegionalCodeMapping.getName();
			HttpResponse<JsonNode> parentCode = Unirest.get("http://219.148.175.179/openapi/t-region/getTRegionDistrictList")
					.queryString("parentCode", code)
					.asJson();
			JsonNode body = parentCode.getBody();
			if(!ObjectUtils.isEmpty(body)){
				String sjson = body.toString();
				if(!ObjectUtils.isEmpty(sjson)){
					JSONObject jsonObject = JSON.parseObject(sjson);
					Object result = jsonObject.get("result");
					if(!ObjectUtils.isEmpty(result)){
						JSONArray array = JSONArray.parseArray(result.toString());
						for (Object o : array) {
							if(!ObjectUtils.isEmpty(o)){
								JSONObject jsonObjectArray = JSON.parseObject(o.toString());
								GgzyQxqCodeMapping codeMapping = new GgzyQxqCodeMapping();
								String id = "";
								Object code1 = jsonObjectArray.get("code");
								if(!ObjectUtils.isEmpty(code1)){
									String scode1 = code1.toString();
									if(!ObjectUtils.isEmpty(scode1)){
										id = id + scode1;
										codeMapping.setCode(scode1);
									}
								}
								Object codePram = jsonObjectArray.get("codePram");
								if(!ObjectUtils.isEmpty(codePram)){
									String codePram1 = codePram.toString();
									if(!ObjectUtils.isEmpty(codePram1)){
										id = id + codePram1;
										codeMapping.setCodePram(codePram1);
									}
								}
								Object platformCode = jsonObjectArray.get("platformCode");
								if(!ObjectUtils.isEmpty(platformCode)){
									String splatformCode = platformCode.toString();
									if(!ObjectUtils.isEmpty(splatformCode)){
										codeMapping.setPlatformCode(splatformCode);
									}
								}
								Object name = jsonObjectArray.get("name");
								if(!ObjectUtils.isEmpty(name)){
									String sname = name.toString();
									if(!ObjectUtils.isEmpty(sname)){
										codeMapping.setName(sname);
									}
								}
								Object norderNumame = jsonObjectArray.get("orderNum");
								if(!ObjectUtils.isEmpty(norderNumame)){
									try {
										String snorderNumame = norderNumame.toString();
										if(!ObjectUtils.isEmpty(snorderNumame)){
											int i = Integer.parseInt(snorderNumame);
											codeMapping.setOrderNum(i);
										}
									}catch (Exception e){
										e.printStackTrace();
									}
								}
								if(!ObjectUtils.isEmpty(id) && id != null && id != ""){
									codeMapping.setId(id);
									if(!ObjectUtils.isEmpty(Pcode)){
										codeMapping.setParentCode(Pcode);
									}
									if(!ObjectUtils.isEmpty(Pname)){
										codeMapping.setParentName(Pname);
									}
									ggzyQxqCodeMappingService.saveOrUpdate(codeMapping);
								}
							}
						}
					}
				}
			}

		}






	* */
	/**
	 *   添加
	 *
	 * @param ggzyQxqCodeMapping
	 * @return
	 */
	@AutoLog(value = "公共资源旗县区映射表-添加")
	@ApiOperation(value="公共资源旗县区映射表-添加", notes="公共资源旗县区映射表-添加")
	@RequiresPermissions("ggzyqxqcodemapping:ggzy_qxq_code_mapping:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GgzyQxqCodeMapping ggzyQxqCodeMapping) {
		ggzyQxqCodeMappingService.save(ggzyQxqCodeMapping);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ggzyQxqCodeMapping
	 * @return
	 */
	@AutoLog(value = "公共资源旗县区映射表-编辑")
	@ApiOperation(value="公共资源旗县区映射表-编辑", notes="公共资源旗县区映射表-编辑")
	@RequiresPermissions("ggzyqxqcodemapping:ggzy_qxq_code_mapping:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GgzyQxqCodeMapping ggzyQxqCodeMapping) {
		ggzyQxqCodeMappingService.updateById(ggzyQxqCodeMapping);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "公共资源旗县区映射表-通过id删除")
	@ApiOperation(value="公共资源旗县区映射表-通过id删除", notes="公共资源旗县区映射表-通过id删除")
	@RequiresPermissions("ggzyqxqcodemapping:ggzy_qxq_code_mapping:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ggzyQxqCodeMappingService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "公共资源旗县区映射表-批量删除")
	@ApiOperation(value="公共资源旗县区映射表-批量删除", notes="公共资源旗县区映射表-批量删除")
	@RequiresPermissions("ggzyqxqcodemapping:ggzy_qxq_code_mapping:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ggzyQxqCodeMappingService.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<GgzyQxqCodeMapping> queryById(@RequestParam(name="id",required=true) String id) {
		GgzyQxqCodeMapping ggzyQxqCodeMapping = ggzyQxqCodeMappingService.getById(id);
		if(ggzyQxqCodeMapping==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ggzyQxqCodeMapping);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ggzyQxqCodeMapping
    */
    @RequiresPermissions("ggzyqxqcodemapping:ggzy_qxq_code_mapping:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GgzyQxqCodeMapping ggzyQxqCodeMapping) {
        return super.exportXls(request, ggzyQxqCodeMapping, GgzyQxqCodeMapping.class, "公共资源旗县区映射表");
    }

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

}
