package cn.cslg.report.service.business;


import cn.cslg.report.common.model.delete.Patents;
import cn.cslg.report.common.model.vo.PatentVO;
import cn.cslg.report.common.utils.Response;

import cn.cslg.report.entity.asso.AssoTaskPersonel;
import cn.cslg.report.mapper.AssoTaskPersonelMapper;

import cn.cslg.report.service.OutInterfaceService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import okhttp3.MediaType;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class AssoTaskPersonelService extends ServiceImpl<AssoTaskPersonelMapper, AssoTaskPersonel> {
   private final  AssoTaskPersonelMapper assoTaskPersonelMapper ;
   private final OutInterfaceService outInterfaceService;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    @Value("${PCSUrl}")
    private String PCSUrl;
    @Value("${PASUrl}")
    private String PASUrl;



    public  void test(){}

    /**
     * @function 根据报告id 删除任务人员关联表对应实体assoTaskPersonel
     * @param reportId
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public String deleAssoTaskPersonel(Integer reportId) throws IOException{
        LambdaQueryWrapper<AssoTaskPersonel> wrappers =new LambdaQueryWrapper();
        wrappers.eq(AssoTaskPersonel::getReportId,reportId);
         this.remove(wrappers);
        return Response.success();
    }

    /**
     * @function：当前登陆人的id状态 任务id 状态 该条专利位置分页处理拿到专利号
     * @param personelId当前登录人的id
     * @param state 状态 1已读 0 未读 3 全部
     * @param taskId 任务id
     * @param location 该条专利位置
     * @throws IOException
     */
    public String pagination(PatentVO patentVO)throws IOException {
        Map<String, Object> map = new HashMap<>();
        List<String> patentNumber = new ArrayList<>();
        if (patentVO.getTaskStatus().equals(3)) {
            //3类型包括1和2
            LambdaQueryWrapper<AssoTaskPersonel> wrappers = new LambdaQueryWrapper();
            wrappers.eq(AssoTaskPersonel::getTaskId, patentVO.getTaskId());
            List<AssoTaskPersonel> list = this.list(wrappers);
            //专利号
            patentNumber = list.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());

        } else {
            //拿到专利号
            LambdaQueryWrapper<AssoTaskPersonel> wrappers = new LambdaQueryWrapper();
            wrappers.eq(AssoTaskPersonel::getTaskId, patentVO.getTaskId())
                    .eq(AssoTaskPersonel::getState, patentVO.getTaskStatus());
            List<AssoTaskPersonel> list = this.list(wrappers);
            //专利号
            patentNumber = list.stream().map(AssoTaskPersonel::getPatentNo).collect(Collectors.toList());
        }

        patentVO.setPatentNos(patentNumber);


        if (patentVO.getLocPosition() == 1) {
            patentVO.setStartNumber(0);
            patentVO.setEndNumber(1);
            String res = outInterfaceService.getPatentFromPAS(patentVO, 1);
            JSONObject jsonObject = JSONObject.parseObject(res);
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            List<JSONObject> jsonObjects = JSONArray.parseArray(jsonObject1.get("records").toString(), JSONObject.class);
           List<Patents> list= new ArrayList<>();

           for(JSONObject obj:jsonObjects){

               Patents patent = obj.toJavaObject(obj, Patents.class);
               list.add(patent);

           }
            if(list.size()==0){

                return Response.success();
            }

           if(list.size()==1){
               map.put("currentID", list.get(0).getId());
               map.put("behindID",null);
               map.put("frontID", null);
               map.put("totalNumber" , list.size());
               map.put("frontPatentNo",null);
               map.put("currentPatentNo",list.get(0).getPatentNo());
               map.put("behindPatentNo",null);
               map.put("frontNumber",0);
               map.put("behindNumber",0);
            return Response.success(map);

           }
            jsonObject1.put("type", "first");
            map.put("currentID", list.get(0).getId());
            map.put("behindID",list.get(1).getId());
            map.put("frontID", null);
            map.put("totalNumber" , list.size());
            map.put("frontNumber",0);
            map.put("behindNumber",list.size()-1);
            map.put("location",1);
            map.put("frontPatentNo",null);
            map.put("currentPatentNo",list.get(0).getPatentNo());
            map.put("behindPatentNo",list.get(1).getPatentNo());
            return  Response.success(map);
        } else {
            patentVO.setStartNumber(patentVO.getLocPosition() - 2);
            patentVO.setEndNumber(patentVO.getLocPosition());
            String res = outInterfaceService.getPatentFromPAS(patentVO, 1);
            JSONObject jsonObject = JSONObject.parseObject(res);
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            List<JSONObject> jsonObjects = JSONArray.parseArray(jsonObject1.get("records").toString(), JSONObject.class);
            List<Patents> list= new ArrayList<>();
            for(JSONObject obj:jsonObjects){
                Patents patent = obj.toJavaObject(obj, Patents.class);
                list.add(patent);

            }

            if(list.size()==1){
                map.put("currentID", list.get(0).getId());
                map.put("behindID",null);
                map.put("frontID", null);
                map.put("totalNumber" , list.size());
                map.put("frontPatentNo",null);
                map.put("currentPatentNo",list.get(0).getPatentNo());
                map.put("behindPatentNo",null);
                map.put("frontNumber",0);
                map.put("behindNumber",0);
                return  Response.success(map);

            }
            if(patentVO.getLocPosition()>=list.size()) {
                map.put("currentID", list.get(list.size()-1).getId());
                map.put("behindID",null);
                map.put("frontID",list.get(list.size()-2).getId());
                map.put("totalNumber" , list.size());
                map.put("frontNumber",list.size()-1);
                map.put("behindNumber",0);
                map.put("location",list.size());
                map.put("frontPatentNo",list.get(list.size()-2).getPatentNo());
                map.put("currentPatentNo",list.get(list.size()-1).getPatentNo());
                map.put("behindPatentNo",null);
                return  Response.success(map);
            }
            map.put("currentID", list.get(patentVO.getLocPosition()-1).getId());
            map.put("behindID",list.get(patentVO.getLocPosition()).getId());
            map.put("frontID",list.get(patentVO.getLocPosition()-2).getId());
            map.put("totalNumber" , list.size());
            map.put("frontNumber",patentVO.getLocPosition()-1);
            map.put("behindNumber", list.size()-patentVO.getLocPosition());
            map.put("location",patentVO.getLocPosition());
            map.put("frontPatentNo",list.get(patentVO.getLocPosition()-2).getPatentNo());
            map.put("currentPatentNo",list.get(patentVO.getLocPosition()-1).getPatentNo());
            map.put("behindPatentNo",list.get(patentVO.getLocPosition()).getPatentNo());
            return  Response.success(map);


        }


    }
}
