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;
    private final ReportFieldService reportFieldService;
    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() {
    }

    /**
     * @param reportId
     * @return
     * @throws IOException
     * @function 根据报告id 删除任务人员关联表对应实体assoTaskPersonel
     */
    @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();
    }

    /**
     * @param personelId当前登录人的id
     * @param state              状态 1已读 0 未读 3 全部
     * @param taskId             任务id
     * @param location           该条专利位置
     * @throws IOException
     * @function：当前登陆人的id状态 任务id 状态 该条专利位置分页处理拿到专利号
     */
    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());
        }
        if(patentVO.getFiledOptions()!=null&&patentVO.getFiledOptions().size()>0) {
            patentNumber = reportFieldService.getPatentNo1(patentVO.getFiledOptions(), patentVO.getReportId(), patentVO.getTaskId(), patentVO.getTaskStatus());
        }

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

}
