package cn.cslg.pas.service.impl;

import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.dto.PatentMarketDataDTO;
import cn.cslg.pas.common.model.vo.PatentNoTrendVO;
import cn.cslg.pas.common.model.vo.PatentTrendVO;
import cn.cslg.pas.common.model.vo.PermissionRecordTrendVO;
import cn.cslg.pas.common.model.vo.ProductMarketDataTrendVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.mapper.PatentMarketDataMapper;
import cn.cslg.pas.service.IPatentMarketDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 专利价值曲线
 *
 * @Author xiexiang
 * @Date 2023/3/15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PatentMarketDataServiceImpl implements IPatentMarketDataService {
    private final PatentMarketDataMapper patentMarketDataMapper;
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;

    @Override
    public List<PatentTrendVO> showTrend(PatentMarketDataDTO dto) {
        PersonnelVO personnelVO =cacheUtils.getLoginUser(loginUtils.getId());
        Integer tenantId =personnelVO.getTenantId();
        //存储专利号，以及专利号查询出来的信息
        List<PatentTrendVO> trendVOS = new ArrayList<>();
        //存储专利号查询出来的所有信息
        List<PatentNoTrendVO> saleVOS = new ArrayList<>();
        //存储根据专利号查询出来的营销数据部分的信息
        List<ProductMarketDataTrendVO> pmdVOS = new ArrayList<>();
        //存储根据专利号查询出来的许可记录部分的信息
        List<PermissionRecordTrendVO> prVOS = new ArrayList<>();
        //判断专利号集合内是否有内容，没有的话直接返回null
        if(dto.getPatentNoList() == null && dto.getPatentNoList().size() == 0) {
            return null;
        }
        if (dto.getTimeUnit() == 2) {//按照年份返回营销数据
            pmdVOS = patentMarketDataMapper.patentQueryByYear(dto.getPatentNoList(), dto.getSaleArea(),tenantId);
            prVOS = patentMarketDataMapper.PRDataQueryByYear(dto.getPatentNoList(),tenantId);
        } else if (dto.getTimeUnit() == 1) {//按照季度返回营销数据
            pmdVOS = patentMarketDataMapper.patentQueryBySeason(dto.getPatentNoList(), dto.getSaleArea(),tenantId);
            prVOS = patentMarketDataMapper.PRDataQueryBySeason(dto.getPatentNoList(),tenantId);
        } else if (dto.getTimeUnit() == null || dto.getTimeUnit() == 0) {//按照月份返回营销数据
            pmdVOS = patentMarketDataMapper.patentQueryByMonth(dto.getPatentNoList(), dto.getSaleArea(),tenantId);
            prVOS = patentMarketDataMapper.PRDataQueryByMonth(dto.getPatentNoList(),tenantId);
        }

        //遍历查询到的许可历史费用
        for (PermissionRecordTrendVO prVO : prVOS) {
            //new一个要返回的类
            PatentNoTrendVO temVO = new PatentNoTrendVO();
            //将prVOS的数据插入到空的retList中
            if(prVO != null) {
                temVO.setMarketDate(prVO.getMarketDate());
                temVO.setLicenseFee(prVO.getLicenseFee());
                saleVOS.add(temVO);
            }
        }

        //遍历专利营销数据费用
        for(ProductMarketDataTrendVO pmdVO:pmdVOS){
            //设置一个标志
            boolean isExistInReturnList = false;
            //遍历返回的retList
            for(PatentNoTrendVO ptVO:saleVOS){
                if(ptVO.getMarketDate().equals(pmdVO.getMarketDate())){
                    //如果找到了pmdVO中和retList中相同的时间
                    isExistInReturnList = true;
                    //将专利营销数据费用赋值给要返回的retList
                    ptVO.setSaleTotalMoney(pmdVO.getSaleTotalMoney());
                    ptVO.setCustomLicenseMoney(pmdVO.getCustomLicenseMoney());
                    break;
                }
            }

            //如果没有找到
            if(!isExistInReturnList){
                PatentNoTrendVO temVO  = new PatentNoTrendVO();
                temVO.setMarketDate(pmdVO.getMarketDate());
                temVO.setSaleTotalMoney(pmdVO.getSaleTotalMoney());
                temVO.setCustomLicenseMoney(pmdVO.getCustomLicenseMoney());
                saleVOS.add(temVO);
            }else{
                isExistInReturnList = false;
            }
        }

        PatentTrendVO trendVO = new PatentTrendVO();
        trendVO.setPatentNoList(dto.getPatentNoList());
        trendVO.setSaleVOS(saleVOS);
        trendVOS.add(trendVO);
        return trendVOS;
    }
}
