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

import java.util.ArrayList;
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 org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.gcproject.entity.GcProject;
import org.jeecg.modules.demo.gcproject.service.IGcProjectService;

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.ggzyannouncementtype.entity.GgzyAnnouncementType;
import org.jeecg.modules.demo.ggzyannouncementtype.service.IGgzyAnnouncementTypeService;
import org.jeecg.modules.demo.ggzycontentdistribution.entity.GgzyContentDistribution;
import org.jeecg.modules.demo.ggzycontentdistribution.entity.GgzyContentDistributionVo;
import org.jeecg.modules.demo.ggzycontentdistribution.service.IGgzyContentDistributionService;
import org.jeecg.modules.demo.ggzyindustrytype.entity.GgzyIndustryType;
import org.jeecg.modules.demo.ggzyindustrytype.service.IGgzyIndustryTypeService;
import org.jeecg.modules.demo.ggzyqxqcodemapping.entity.GgzyQxqCodeMapping;
import org.jeecg.modules.demo.ggzyqxqcodemapping.service.IGgzyQxqCodeMappingService;
import org.jeecg.modules.demo.ggzyregionalcodemapping.entity.GgzyRegionalCodeMapping;
import org.jeecg.modules.demo.ggzyregionalcodemapping.service.IGgzyRegionalCodeMappingService;
import org.jeecg.modules.elasticsearch.entity.TenderDetailsVo;
import org.jeecg.modules.utils.RandomStrUtil;
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-10
 * @Version: V1.0
 */
@Api(tags="项目")
@RestController
@RequestMapping("/gcproject/gcProject")
@Slf4j
public class GcProjectController extends JeecgController<GcProject, IGcProjectService> {
	@Autowired
	private IGcProjectService gcProjectService;
	 @Autowired
	 private IGgzyContentDistributionService ggzyContentDistributionService;

	@Autowired
	private IGgzyContentDistributionService iGgzyContentDistributionService;

	 @Autowired
	 private IGgzyRegionalCodeMappingService iGgzyRegionalCodeMappingService;


	 @Autowired
	 private IGgzyQxqCodeMappingService iGgzyQxqCodeMappingService;

	 @Autowired
	 private IGgzyAnnouncementTypeService iGgzyAnnouncementTypeService;

	 @Autowired
	 private IGgzyIndustryTypeService iGgzyIndustryTypeService;
	/**
	 * 分页列表查询
	 *
	 * @param gcProject
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "项目-分页列表查询")
	@ApiOperation(value="项目-分页列表查询", notes="项目-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GcProject>> queryPageList(GcProject gcProject,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		System.out.println(gcProject);
		QueryWrapper<GcProject> queryWrapper = QueryGenerator.initQueryWrapper(gcProject, req.getParameterMap());
		Page<GcProject> page = new Page<GcProject>(pageNo, pageSize);
		IPage<GcProject> pageList = gcProjectService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param gcProject
	 * @return
	 */
	@AutoLog(value = "项目-添加")
	@ApiOperation(value="项目-添加", notes="项目-添加")
	@RequiresPermissions("gcproject:gc_project:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GcProject gcProject) {
		gcProjectService.save(gcProject);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param gcProject
	 * @return
	 */
	@AutoLog(value = "项目-编辑")
	@ApiOperation(value="项目-编辑", notes="项目-编辑")
	@RequiresPermissions("gcproject:gc_project:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GcProject gcProject) {
		gcProjectService.updateById(gcProject);

		String id = gcProject.getId();
		String projectCode = gcProject.getProjectCode();
		if(!ObjectUtils.isEmpty(id)){
			QueryWrapper<GgzyContentDistribution> queryWrapper = new QueryWrapper<GgzyContentDistribution>();
			queryWrapper.eq("project_code",projectCode);
			long count = iGgzyContentDistributionService.count(queryWrapper);
			Integer pageNo = 1;
			Integer pageSize = 100;
			Integer pageNum = (int)Math.ceil((double)count/pageSize);
			for(int i = 1; i <= pageNum;i++){
				try {
					Page<GgzyContentDistribution> page = new Page<GgzyContentDistribution>(pageNo, pageSize);
					IPage<GgzyContentDistribution> pageList = iGgzyContentDistributionService.page(page, queryWrapper);
					if(!ObjectUtils.isEmpty(pageList)){
						List<GgzyContentDistribution> records = pageList.getRecords();
						if(!ObjectUtils.isEmpty(records)){
							if(records.size() >= 1){
								for (GgzyContentDistribution ggzyContentDistribution : records) {
									ggzyContentDistribution.setIsSynchronous("4");
									editGgzyContentDistribution(ggzyContentDistribution);
								}
							}
						}
					}
				}catch (Exception e){
					e.printStackTrace();

				}
			}
		}
		return Result.OK("编辑成功!");
	}

	 public void editGgzyContentDistribution(GgzyContentDistribution ggzyContentDistribution) {
		 String projectCode = ggzyContentDistribution.getProjectCode();
		 if(!ObjectUtils.isEmpty(projectCode) && projectCode != ""){
			 GcProject gcProject = getGcProject(projectCode);
			 if(!ObjectUtils.isEmpty(gcProject)){
				 String regionCode = gcProject.getRegionCode();
				 if(!ObjectUtils.isEmpty(regionCode) && regionCode != ""){
					 ggzyContentDistribution.setRegionCode(regionCode);
					 GgzyQxqCodeMapping ggzyQxqCodeMapping = getGgzyQxqCodeMapping(regionCode);
					 if(!ObjectUtils.isEmpty(ggzyQxqCodeMapping)){
						 String name = ggzyQxqCodeMapping.getName();
						 if(!ObjectUtils.isEmpty(name)){
							 ggzyContentDistribution.setRegionName(name);
						 }
					 }
				 }
				 String platformCode = gcProject.getPlatformCode();
				 if(!ObjectUtils.isEmpty(platformCode) && platformCode != ""){
					 ggzyContentDistribution.setPlatformCode(platformCode);
					 GgzyRegionalCodeMapping ggzyRegionalCodeMapping = getGgzyRegionalCodeMapping(platformCode);
					 if(!ObjectUtils.isEmpty(ggzyRegionalCodeMapping)){
						 String name = ggzyRegionalCodeMapping.getName();
						 if(!ObjectUtils.isEmpty(name)){
							 ggzyContentDistribution.setPlatformName(name);
						 }
					 }
				 }
				 String projectName = gcProject.getProjectName();
				 if(!ObjectUtils.isEmpty(projectName)){
					 ggzyContentDistribution.setProjectName(projectName);
				 }
				 String industriesTypeCode = gcProject.getIndustriesType();
				 if(!ObjectUtils.isEmpty(industriesTypeCode)){
					 ggzyContentDistribution.setIndustriesTypeCode(industriesTypeCode);

					 GgzyIndustryType ggzyIndustryType = getGgzyIndustryType(industriesTypeCode);
					 if(!ObjectUtils.isEmpty(ggzyIndustryType)){
						 String industriesTypeName = ggzyIndustryType.getIndustriesTypeName();
						 ggzyContentDistribution.setIndustriesTypeName(industriesTypeName);
					 }
				 }

			 }
		 }
		 String regionCode = ggzyContentDistribution.getRegionCode();
		 if(!ObjectUtils.isEmpty(regionCode) && regionCode != ""){
			 GgzyQxqCodeMapping ggzyQxqCodeMapping = getGgzyQxqCodeMapping(regionCode);
			 if(!ObjectUtils.isEmpty(ggzyQxqCodeMapping)){
				 String name = ggzyQxqCodeMapping.getName();
				 if(!ObjectUtils.isEmpty(name)){
					 ggzyContentDistribution.setRegionName(name);
				 }
			 }
		 }
		 String platformCode = ggzyContentDistribution.getPlatformCode();
		 if(!ObjectUtils.isEmpty(platformCode) && platformCode != ""){
			 GgzyRegionalCodeMapping ggzyRegionalCodeMapping = getGgzyRegionalCodeMapping(platformCode);
			 if(!ObjectUtils.isEmpty(ggzyRegionalCodeMapping)){
				 String name = ggzyRegionalCodeMapping.getName();
				 if(!ObjectUtils.isEmpty(name)){
					 ggzyContentDistribution.setPlatformName(name);
				 }
			 }
		 }
		 String noticeTypeCodeId = ggzyContentDistribution.getNoticeTypeCodeId();
		 if(!ObjectUtils.isEmpty(noticeTypeCodeId) && noticeTypeCodeId != ""){
			 GgzyAnnouncementType ggzyAnnouncementTypeById = getGgzyAnnouncementTypeById(noticeTypeCodeId);
			 if(!ObjectUtils.isEmpty(ggzyAnnouncementTypeById)){
				 String noticeTypeCode = ggzyAnnouncementTypeById.getNoticeTypeCode();
				 if(!ObjectUtils.isEmpty(noticeTypeCode)){
					 ggzyContentDistribution.setNoticeTypeCode(noticeTypeCode);
				 }
				 String noticeTypeName = ggzyAnnouncementTypeById.getNoticeTypeName();
				 if(!ObjectUtils.isEmpty(noticeTypeName)){
					 ggzyContentDistribution.setNoticeTypeName(noticeTypeName);
				 }
				 String transactionTypeCode = ggzyAnnouncementTypeById.getTransactionTypeCode();
				 if(!ObjectUtils.isEmpty(transactionTypeCode)){
					 ggzyContentDistribution.setTransactionTypeCode(transactionTypeCode);
				 }
				 String transactionTypeName = ggzyAnnouncementTypeById.getTransactionTypeName();
				 if(!ObjectUtils.isEmpty(transactionTypeName)){
					 ggzyContentDistribution.setTransactionTypeName(transactionTypeName);
				 }
			 }
		 }
		 String industriesTypeCode = ggzyContentDistribution.getIndustriesTypeCode();
		 if(!ObjectUtils.isEmpty(industriesTypeCode)){
			 GgzyIndustryType ggzyIndustryType = getGgzyIndustryType(industriesTypeCode);
			 if(!ObjectUtils.isEmpty(ggzyIndustryType)){
				 String industriesTypeName = ggzyIndustryType.getIndustriesTypeName();
				 ggzyContentDistribution.setIndustriesTypeName(industriesTypeName);
			 }
		 }
		 ggzyContentDistributionService.saveOrUpdate(ggzyContentDistribution);
	 }

	 @GetMapping(value = "/getSourceDataKey")
	 public Result<String> getSourceDataKey(){
		 String randomNumberNew = null;
		 try {
			 for (int i = 0; i < 10; i++) {
				 String randomNumber = RandomStrUtil.getRandomNumber(20);
				 QueryWrapper<GcProject> queryWrapper = new QueryWrapper<GcProject>();
				 queryWrapper.eq("source_data_key",randomNumber);
				 long count = gcProjectService.count(queryWrapper);
				 if(count <= 0){
					 randomNumberNew = randomNumber;
					 break;
				 }
			 }
		 }catch (Exception e){
			 e.printStackTrace();
		 }
		 if(ObjectUtils.isEmpty(randomNumberNew)){
			 return Result.ok("有重复内容请重新生成");
		 }
		 return Result.ok(randomNumberNew);
	 }


	 @GetMapping(value = "/getProjectCode")
	 public Result<String> getProjectCode(){
		 String randomNumberNew = null;
		 try {
			 for (int i = 0; i < 10; i++) {
				 String randomNumber = RandomStrUtil.getRandomNumberCrossBar(15);
				 QueryWrapper<GcProject> queryWrapper = new QueryWrapper<GcProject>();
				 queryWrapper.eq("project_code",randomNumber);
				 long count = gcProjectService.count(queryWrapper);
				 if(count <= 0){
					 randomNumberNew = randomNumber;
					 break;
				 }
			 }
		 }catch (Exception e){
			 e.printStackTrace();
		 }
		 if(ObjectUtils.isEmpty(randomNumberNew)){
			 return Result.ok("有重复内容请重新生成");
		 }
		 return Result.ok(randomNumberNew);
	 }

	 /**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目-通过id删除")
	@ApiOperation(value="项目-通过id删除", notes="项目-通过id删除")
	@RequiresPermissions("gcproject:gc_project:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		gcProjectService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目-批量删除")
	@ApiOperation(value="项目-批量删除", notes="项目-批量删除")
	@RequiresPermissions("gcproject:gc_project:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gcProjectService.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<GcProject> queryById(@RequestParam(name="id",required=true) String id) {
		GcProject gcProject = gcProjectService.getById(id);
		if(gcProject==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gcProject);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gcProject
    */
    @RequiresPermissions("gcproject:gc_project:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GcProject gcProject) {
        return super.exportXls(request, gcProject, GcProject.class, "项目");
    }

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

	 private GgzyAnnouncementType getGgzyAnnouncementTypeById(String id){
		 GgzyAnnouncementType byId = iGgzyAnnouncementTypeService.getById(id);
		 return byId;
	 }

	 private GgzyQxqCodeMapping getGgzyQxqCodeMapping(String codePram){
		 QueryWrapper<GgzyQxqCodeMapping> queryWrapper = new QueryWrapper<GgzyQxqCodeMapping>();
		 Page<GgzyQxqCodeMapping> page = new Page<GgzyQxqCodeMapping>(1, 1);
		 queryWrapper.eq("code_pram",codePram);
		 IPage<GgzyQxqCodeMapping> pageList = iGgzyQxqCodeMappingService.page(page, queryWrapper);
		 if(!ObjectUtils.isEmpty(pageList)){
			 List<GgzyQxqCodeMapping> records = pageList.getRecords();
			 if(!ObjectUtils.isEmpty(records)){
				 int size = records.size();
				 if(size >= 1){
					 GgzyQxqCodeMapping codeMapping = records.get(0);
					 if(!ObjectUtils.isEmpty(codeMapping)){
						 return codeMapping;
					 }
				 }
			 }
		 }
		 return null;
	 }

	 private GgzyRegionalCodeMapping getGgzyRegionalCodeMapping(String platformCode){
		 QueryWrapper<GgzyRegionalCodeMapping> queryWrapper = new QueryWrapper<GgzyRegionalCodeMapping>();
		 Page<GgzyRegionalCodeMapping> page = new Page<GgzyRegionalCodeMapping>(1, 1);
		 queryWrapper.eq("platform_code",platformCode);
		 IPage<GgzyRegionalCodeMapping> pageList = iGgzyRegionalCodeMappingService.page(page, queryWrapper);
		 if(!ObjectUtils.isEmpty(pageList)){
			 List<GgzyRegionalCodeMapping> records = pageList.getRecords();
			 if(!ObjectUtils.isEmpty(records)){
				 int size = records.size();
				 if(size >= 1){
					 GgzyRegionalCodeMapping codeMapping = records.get(0);
					 if(!ObjectUtils.isEmpty(codeMapping)){
						 return codeMapping;
					 }
				 }
			 }
		 }
		 return null;
	 }

	 private GcProject getGcProject(String projectCode){
		 QueryWrapper<GcProject> queryWrapper = new QueryWrapper<GcProject>();
		 Page<GcProject> page = new Page<GcProject>(1, 1);
		 queryWrapper.eq("project_code",projectCode);
		 IPage<GcProject> pageList = gcProjectService.page(page, queryWrapper);
		 if(!ObjectUtils.isEmpty(pageList)){
			 List<GcProject> records = pageList.getRecords();
			 if(!ObjectUtils.isEmpty(records)){
				 int size = records.size();
				 if(size >= 1){
					 GcProject gcProject = records.get(0);
					 return gcProject;
				 }
			 }
		 }
		 return null;
	 }


	 private GgzyIndustryType getGgzyIndustryType(String industryTypeCode){
		 QueryWrapper<GgzyIndustryType> queryWrapper = new QueryWrapper<GgzyIndustryType>();
		 Page<GgzyIndustryType> page = new Page<GgzyIndustryType>(1, 1);
		 queryWrapper.eq("industries_type_code",industryTypeCode);
		 IPage<GgzyIndustryType> pageList = iGgzyIndustryTypeService.page(page, queryWrapper);
		 if(!ObjectUtils.isEmpty(pageList)){
			 List<GgzyIndustryType> records = pageList.getRecords();
			 if(!ObjectUtils.isEmpty(records)){
				 int size = records.size();
				 if(size >= 1){
					 GgzyIndustryType ggzyIndustryType = records.get(0);
					 return ggzyIndustryType;
				 }
			 }
		 }
		 return null;
	 }

 }
