package com.jeeplus.test.cw.common.enterpriseSearch;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("企业信息查询API")
@RestController
@RequestMapping(value = "/enterprise_search_api")
public class EnterpriseSearchApi {

    /**
     * 根据关键字查询相关企业
     * @param keyword
     * @return
     */
    @ApiOperation(value = "根据关键字查询相关企业")
    @PostMapping("enterpriseSearchByName")
    public String enterpriseSearchByName(@RequestParam String keyword){
        if(StringUtils.isBlank(keyword)){
            return null;
        }
        return  TicketQueryUtils.enterpriseSearchByName(keyword);
    }

    /**
     * 根据id查询企业税号
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询企业税号")
    @PostMapping("enterpriseTicketInfo")
    public Object enterpriseTicketInfo(String id){
        if(StringUtils.isBlank(id)){
            return null;
        }
        return TicketQueryUtils.enterpriseTicketInfoQueryById(id);
    }

    /**
     * 分页查询相关企业
     * @param enterpriseSearchDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("分页查询相关企业")
    @GetMapping("list")
    public ResponseEntity<IPage<EnterpriseSearchDTO>> data(EnterpriseSearchDTO enterpriseSearchDTO, Page<EnterpriseSearchDTO> page) throws Exception {
        IPage<EnterpriseSearchDTO> result = new Page<EnterpriseSearchDTO>();
        List<EnterpriseSearchDTO> list = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(enterpriseSearchDTO)){
            if (StringUtils.isNotBlank(enterpriseSearchDTO.getENTNAME())){
                String s = TicketQueryUtils.enterpriseSearchPage(enterpriseSearchDTO.getENTNAME(),String.valueOf(page.getSize()),String.valueOf(page.getCurrent()));
                if (StringUtils.isNotBlank(s)){
                    EnterpriseJsonRespDTO enterpriseJsonRespDTO = JSONObject.parseObject(s, EnterpriseJsonRespDTO.class); //json字符串直接转java对象
                    if (ObjectUtil.isNotEmpty(enterpriseJsonRespDTO)){
                        if(ObjectUtil.isNotEmpty(enterpriseJsonRespDTO.getData())){
                            if(CollectionUtil.isNotEmpty(enterpriseJsonRespDTO.getData().getItems())){
                                enterpriseJsonRespDTO.getData().getItems().stream().forEach(item->{
                                    String s1 = TicketQueryUtils.enterpriseTicketInfoQueryById(item.getCompanyid());
                                    EnterpriseSearchDTO dto = JSONObject.parseObject(s1, EnterpriseSearchDTO.class);
                                    list.add(dto);
                                });
                                result.setRecords(list);
                            }
                        }
                    }
                }
            }
        }

        return ResponseEntity.ok (result);
    }

}
