package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.PatentAuthorizationMapper;
import com.lightinit.hsdatashow.mapper.RdPersonnelMapper;
import com.lightinit.hsdatashow.mapper.ScientistsEngineersMapper;
import com.lightinit.hsdatashow.mapper.TechnicalProductsMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.SpreadListModel;
import com.lightinit.hsdatashow.service.admin.IInnovateService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Service
public class InnovateServiceImpl implements IInnovateService {
    @Autowired
    private RdPersonnelMapper rdPersonnelMapper;
    @Autowired
    private TechnicalProductsMapper technicalProductsMapper;
    @Autowired
    private PatentAuthorizationMapper patentAuthorizationMapper;
    @Autowired
    private ScientistsEngineersMapper scientistsEngineersMapper;
    @Override
    public SpreadListModel getRDpersonnel() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<RdPersonnel> list =rdPersonnelMapper.selectList(year);
        SpreadListModel model = new SpreadListModel();
        List<BigDecimal> jichu = new ArrayList<>();
        List<BigDecimal> yingyong = new ArrayList<>();
        List<BigDecimal> shiyan = new ArrayList<>();
        List<BigDecimal> zong = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (RdPersonnel rdPersonnel:list
                ) {

            if(!dataYear.contains(rdPersonnel.getDataYear())) {
                dataYear.add(rdPersonnel.getDataYear());
            }
            if(!name.contains(rdPersonnel.getName())) {
                name.add(rdPersonnel.getName());
            }
            if("基础研究".equals(rdPersonnel.getName())) {
                jichu.add(rdPersonnel.getValue());
            }
            if("应用研究".equals(rdPersonnel.getName())) {
                yingyong.add(rdPersonnel.getValue());
            }
            if("实验发展".equals(rdPersonnel.getName())) {
                shiyan.add(rdPersonnel.getValue());
            }
        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setReads(jichu);
        model.setWifi(yingyong);
        model.setSiG(shiyan);
        model.setTelevision(zong);
        return model;
    }

    @Override
    public ResultState getRDpersonnel1() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-3;
        List<RdPersonnel> list =rdPersonnelMapper.selectList(year);
        resultState.setData(list);
        return resultState;
    }

    @Override
    public SpreadListModel getTechnicalProducts() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<TechnicalProducts> list =technicalProductsMapper.selectList(year);
        SpreadListModel model = new SpreadListModel();
        List<BigDecimal> zong = new ArrayList<>();
        List<BigDecimal> jishu = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (TechnicalProducts technicalProducts:list
                ) {

            if(!dataYear.contains(technicalProducts.getDataYear())) {
                dataYear.add(technicalProducts.getDataYear());
            }
            if(!name.contains(technicalProducts.getNmae())) {
                name.add(technicalProducts.getNmae());
            }
            if("总出口".equals(technicalProducts.getNmae())) {
                zong.add(technicalProducts.getValue());
            }
            if("高技术产品".equals(technicalProducts.getNmae())) {
                jishu.add(technicalProducts.getValue());
            }

        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setReads(jishu);
        model.setWifi(zong);

        return model;
    }

    @Override
    public ResultState getPatentAuthorization() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-2 ;
        List<PatentAuthorization> list = patentAuthorizationMapper.selectList(year);
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (PatentAuthorization chartModel : list) {
                names.add(chartModel.getName());
            }
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public SpreadListModel getPatentAuthorization1() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<PatentAuthorization> list =patentAuthorizationMapper.selectList(year);
        SpreadListModel model = new SpreadListModel();
        List<BigDecimal> faming = new ArrayList<>();
        List<BigDecimal> sheji = new ArrayList<>();
        List<BigDecimal> shiyong = new ArrayList<>();
        List<BigDecimal> zong = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (PatentAuthorization technicalProducts:list
                ) {

            if(!dataYear.contains(technicalProducts.getDataYear())) {
                dataYear.add(technicalProducts.getDataYear());
            }
            if(!name.contains(technicalProducts.getName())) {
                name.add(technicalProducts.getName());
            }
            if("发明".equals(technicalProducts.getName())) {
                faming.add(technicalProducts.getValue());
            }
            if("外观设计".equals(technicalProducts.getName())) {
                sheji.add(technicalProducts.getValue());
            }
            if("使用新型".equals(technicalProducts.getName())) {
                shiyong.add(technicalProducts.getValue());
            }

        }
        model.setDataYear(dataYear);
        model.setName(name);
        model.setReads(shiyong);
        model.setWifi(sheji);
        model.setSiG(faming);
        return model;
    }

    @Override
    public SpreadListModel getScientistsEngineers() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<ScientistsEngineers> list =scientistsEngineersMapper.selectList(year);
        SpreadListModel model = new SpreadListModel();

        List<BigDecimal> siG = new ArrayList<>();
        List<Integer> dataYear = new ArrayList<>();
        for (ScientistsEngineers technicalProducts:list
                ) {

                dataYear.add(technicalProducts.getDataYear());

                siG.add(technicalProducts.getValue());

        }
        model.setDataYear(dataYear);
        model.setSiG(siG);
        return model;
    }
}
