package com.sky.ioc.service.assets.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.entity.Indeicator;
import com.sky.ioc.entity.Label;
import com.sky.ioc.entity.Cake;
import com.sky.ioc.entity.domain.operation.Asset;
import com.sky.ioc.entity.domain.operation.Investment;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.mapper.operation.AssetMapper;
import com.sky.ioc.mapper.operation.InvestmentMapper;
import com.sky.ioc.service.assets.AssetService;
import com.sky.ioc.tool.DateUtil;
import com.sky.ioc.tool.GeneralMethod;
import com.sky.ioc.tool.MathUtils;
import com.sky.ioc.tool.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * TODO
 * @Description 资产
 * @Author LunCe
 * @Date 2023/2/23 17:04
 **/

@Service
public class AssetServiceImpl implements AssetService {

    @Autowired
    AssetMapper assetMapper;

    @Autowired
    InvestmentMapper investmentMapper;

    @Override
    public ReturnMsg getInvestmentDistribution() {
        String[] param = {"investment1","investment2","investment3","investment4","rateReturn"};
        String[] labKey = {"1季度","2季度","3季度","4季度"};
        ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Label> list = new ArrayList<>();
        Label label = new Label();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(param[0],767);
        jsonObject.put(param[1],300);
        jsonObject.put(param[2],489);
        jsonObject.put(param[3],612);
        jsonObject.put(param[4],54);
        label.setJsonObject(jsonObject);
        label.setLabel(labKey[0]);
        list.add(label);

        Label label1 = new Label();
        JSONObject jsonObject1 = new JSONObject();

        jsonObject1.put(param[0],398);
        jsonObject1.put(param[1],621);
        jsonObject1.put(param[2],489);
        jsonObject1.put(param[3],730);
        jsonObject1.put(param[4],63);
        label1.setJsonObject(jsonObject);
        label1.setLabel(labKey[1]);
        list.add(label1);

        Label label2 = new Label();
        JSONObject jsonObject2 = new JSONObject();

        jsonObject2.put(param[0],498);
        jsonObject2.put(param[1],439);
        jsonObject2.put(param[2],500);
        jsonObject2.put(param[3],300);
        jsonObject2.put(param[4],40);
        label2.setJsonObject(jsonObject2);
        label2.setLabel(labKey[2]);
        list.add(label2);

        Label label3 = new Label();
        JSONObject jsonObject3 = new JSONObject();

        jsonObject3.put(param[0],550);
        jsonObject3.put(param[1],480);
        jsonObject3.put(param[2],620);
        jsonObject3.put(param[3],600);
        jsonObject3.put(param[4],70);
        label3.setJsonObject(jsonObject3);
        label3.setLabel(labKey[3]);
        list.add(label3);
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getAssetLife() {
        String[] param = {"serviceLife","standardLife"};
        String[] labKey = {"资产1","资产2","资产3","资产4","资产5","资产6"};
        ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Label> list = new ArrayList<>();
        Label label1 = new Label();
        JSONObject jsonObject = new JSONObject();
        int i2 = 5;
        int i1 = 3;
        jsonObject.put(param[0],i1);
        jsonObject.put(param[1],i2);
        label1.setJsonObject(jsonObject);
        label1.setLabel(labKey[0]);
        list.add(label1);
        Label label2 = new Label();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put(param[0],2);
        jsonObject2.put(param[1],5);
        label2.setJsonObject(jsonObject2);
        label2.setLabel(labKey[1]);
        list.add(label2);
        Label label3 = new Label();
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put(param[0],4);
        jsonObject3.put(param[1],9);
        label3.setJsonObject(jsonObject);
        label3.setLabel(labKey[2]);
        list.add(label3);
        Label label4 = new Label();
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put(param[0],3);
        jsonObject4.put(param[1],6);
        label4.setJsonObject(jsonObject4);
        label4.setLabel(labKey[3]);
        list.add(label4);
        Label label5 = new Label();
        JSONObject jsonObject5 = new JSONObject();
        jsonObject5.put(param[0],5);
        jsonObject5.put(param[1],8);
        label5.setJsonObject(jsonObject5);
        label5.setLabel(labKey[4]);
        list.add(label5);
      //  List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getAssetCount() {
        Map<String, Object> map = new HashMap<>();
        map.put("yearCount","12000");
        map.put("yearCountYoy","0.4%");
        map.put("totalAssetCount","2100");
        map.put("totalAssetCountYoy","0.2%");
        map.put("totalAssetsPerSapita","15");
        map.put("averageServiceLifeOfAssets","10");
        map.put("worthAttention","内容");
        return  ReturnMsg.ok(map);
    }

    @Override
    public ReturnMsg getOperAssetCoreIndicators(IocParam iocParam) {

        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        double totalAsset = assetMapper.getTotalWorth(iocParam);
        int assetCount =  assetMapper.getTotalCount(iocParam);
        float compare = (float) Math.random();
        compare = Float.parseFloat(decimalFormat.format(compare));
        List<Asset> assets = assetMapper.topList(iocParam);
        String assetName = "";
        if(assets.size()>0){
            assetName = assets.get(0).getName();
        }
        String investmentName = "";
        Investment investment = investmentMapper.getTopOne(iocParam);
        if(investment!=null){
            investmentName = investment.getField();
        }
        objects.get(0).put("value", MathUtils.division(totalAsset,1,2));
        objects.get(0).put("compare",compare);

        objects.get(1).put("value",assetCount);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",assetName);
        objects.get(2).put("compare",60);

        objects.get(3).put("value",investmentName);
        objects.get(3).put("compare",60);
        indeicator.setWorthAttention("13笔记本即将到报废年限");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAssetAnalysis(IocParam iocParam) {
       // String[] param = {"investment1","investment2","investment3","investment4","rateReturn"};
        List<Investment> investments = investmentMapper.listByTime(iocParam);
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:00");
            iocParam1.setTimeRange(iocTimeRange);
            JSONObject jsonObject = new JSONObject();
            for (int j = 0; j < investments.size(); j++) {
               // int i1 = random.nextInt(50, 150);
                String name = investments.get(j).getName();
                Investment investment = investmentMapper.getByTimeAndName(iocParam1,name);
                if(investment!=null){
                    jsonObject.put("investment"+j,investment.getMoney());
                }else{
                    jsonObject.put("investment"+j,0);
                }

            }
            double rateReturn = investmentMapper.avgReturnByTime(iocParam1);
            jsonObject.put("rateReturn",MathUtils.division(rateReturn,1,2));
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }

       // List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getAssetShareRanking(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Integer> datas = new ArrayList<>();
        Integer total = 0;
       for(int i=0;i<6;i++){
           Integer data = random.nextInt(50,150);
           datas.add(data);
           total+=data;
       }
       for(int i=0;i<datas.size();i++){
           Cake cake = new Cake();
           int j = i+1;
           cake.setName("资产"+j);
           double  p = MathUtils.division(datas.get(i),total,4)*100;
           cake.setValue(MathUtils.division(p,1,2));
           list.add(cake);

       }

        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getScrapAndMaintain(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        list.add(new Cake("其他部门"));
        list.add(new Cake("管理部门"));
        list.add(new Cake("销售部门"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getAssetCoreIndicators() {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        ThreadLocalRandom random = ThreadLocalRandom.current();
     /*   int yearAsset = random.nextInt(8000, 10000);//年度
        int  personAsset= random.nextInt(50,100);//人均
        int totalyear = random.nextInt(2,8);
        int totalAsset = random.nextInt(150000, 300000);*/
        float compare = (float) Math.random();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        compare = Float.parseFloat(decimalFormat.format(compare));

        objects.get(0).put("value",1200);
        objects.get(0).put("compare",compare);

        objects.get(1).put("value",12000);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",120);
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",120);
        objects.get(3).put("compare",compare);
        indeicator.setWorthAttention("内容");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAssetAge(IocParam iocParam) {
        String[] param = {"assetsName","rateReturn"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getAssetNumber() {
        String[] param = {"固定资产一级分类","平均使用时间"};
        String[] labKey = {"办公类","局房类","个人办公类"};
       // ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Label> list = new ArrayList<>();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            int i1 = random.nextInt(2, 10);
            jsonObject.put(param[1],i1);
            int i0 = random.nextInt(30, 150);
            jsonObject.put(param[0],i0);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            list.add(label);
        }
        return ReturnMsg.ok(list);
    }
}
