package com.boot.security.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.boot.security.server.dao.CopyrightDao;
import com.boot.security.server.model.BaseSettlement;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.service.CopyrightBookService;
import com.boot.security.server.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Stream;

@Service
public class CopyrightBookServiceImpl implements CopyrightBookService {
    @Autowired
    CopyrightDao copyrightDao;


    @Override
    public String selectSettlement(Integer copyrightId,Integer page,Integer limit) {
        Integer roleId = copyrightDao.selectRoleId(UserUtil.getLoginUser().getId());
        Integer status=null;
        List<BaseSettlement> baseSettlementList =new ArrayList<>();
        if(copyrightId==null||copyrightId==0){
            baseSettlementList = copyrightDao.selectCopyrightById(UserUtil.getLoginUser().getId());
        }else {
            BaseSettlement baseSettlement = new BaseSettlement();
            baseSettlement.setCopyrightId(copyrightId);
            baseSettlementList.add(baseSettlement);
        }
        if(roleId!=3){
            status=1;
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectTableListMonth(baseSettlementList,page-1,limit,status);

        DecimalFormat df = new DecimalFormat("#.00");
        for (BaseSettlement baseSettlement : baseSettlements) {
            Integer type = baseSettlement.getType();
            Double totalAmount = baseSettlement.getTotalAmount();
            String context = baseSettlement.getContext();
            Double count=0.0;
            Double count1=0.0;
            Double totalAdvertAmount = baseSettlement.getTotalAdvertAmount();
            //扣量算法
            if(type==1){
                JSONObject jsonObject = JSONObject.parseObject(context);
                Set<String> keySet = jsonObject.keySet();
                ArrayList<Integer> list = new ArrayList<>();
                list.add(0);
                for (String s : keySet) {
                    list.add(Integer.valueOf(s));
                }
                Collections.sort(list);
                int size = list.size();
                int flag=0;
                int flag1=0;
                Double a1=0D;
                Double a2=0D;
                for(int i=1;i<size;i++){
                    if(totalAmount>=list.get(i-1)&&totalAmount<=list.get(i)){
                       flag=i;
                       a1=totalAmount-list.get(i-1);
                       count=a1*jsonObject.getDouble(String.valueOf( list.get(i) ) );
                       break;
                    }
                }

                for(int i=1;i<size;i++){
                    if(totalAdvertAmount>=list.get(i-1)&&totalAdvertAmount<=list.get(i)){
                        flag1=i;
                        a2=totalAdvertAmount-list.get(i-1);
                        count1=a2*jsonObject.getDouble(String.valueOf( list.get(i) ) );
                        break;
                    }
                }

                for(int i=1;i<flag;i++){
                    count+=list.get(i)*jsonObject.getDouble(String.valueOf( list.get(i) ));
                }

                for(int i=1;i<flag1;i++){
                    count1+=list.get(i)*jsonObject.getDouble(String.valueOf( list.get(i) ));
                }

            }else {

                count=totalAmount*Double.valueOf(context);

                count1=totalAmount*Double.valueOf(context);

            }

            count=Double.valueOf(df.format(count));
            baseSettlement.setTotalAmount(count);
            baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));


            count=Double.valueOf(df.format(count1));
            Integer id = baseSettlement.getCopyrightId();
            if(id==3219||id==3230||id==3254||id==3253){
                count=Double.valueOf(df.format(baseSettlement.getTotalAmount()+count));
                baseSettlement.setTotalAmount(count);
                baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));
                baseSettlement.setTotalAdvertAmount(0d);
                baseSettlement.setSettlementAdAmount(0d);
            }else {
                baseSettlement.setTotalAdvertAmount(count);
                baseSettlement.setSettlementAdAmount(Double.valueOf(df.format(count*0.5)));
            }
            baseSettlement.setCountSettlement(Double.valueOf(df.format(baseSettlement.getSettlementAmount()+baseSettlement.getSettlementAdAmount())));
        }
        Integer count = copyrightDao.selectTableListMonthCount(baseSettlementList,status);
        return PageTableHandler.getJSONObject(baseSettlements,count);
    }

    @Override
    public String selectBookSettlement(Integer copyrightId,String startTime,String endTime,Long bookId,String bookName,Integer page,Integer limit) {
        Integer roleId = copyrightDao.selectRoleId(UserUtil.getLoginUser().getId());
        List<BaseSettlement> baseSettlementList =new ArrayList<>();
        if(copyrightId==null||copyrightId==0){
            baseSettlementList = copyrightDao.selectCopyrightById(UserUtil.getLoginUser().getId());
        }else {
            BaseSettlement baseSettlement = new BaseSettlement();
            baseSettlement.setCopyrightId(copyrightId);
            baseSettlementList.add(baseSettlement);
        }

        List<BaseSettlement> list =new ArrayList<>();

        if(roleId!=3){
            list = copyrightDao.selectReleaseCopyright();
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectBookDetail(baseSettlementList,bookId,bookName,startTime,endTime,page-1,limit);
        DecimalFormat df = new DecimalFormat("#.00");
        Iterator<BaseSettlement> iterator = baseSettlements.iterator();
        while (iterator.hasNext()) {
            BaseSettlement baseSettlement = iterator.next();
            Boolean flag=false;
            for (BaseSettlement settlement : list) {
                Integer id = settlement.getCopyrightId();
                Integer id1 = baseSettlement.getCopyrightId();
                if( (settlement.getReportDate().equals(baseSettlement.getReportDate()) ) && id.equals(id1) ){
                    flag=true;
                    break;
                }
            }
            if(roleId!=3){
                if(list==null||list.size()==0){
                    baseSettlements.clear();
                    break;
                }
            }
            if (flag==false&&list.size()>0){
                iterator.remove();
                continue;
            }
            if(baseSettlement.getTotalAmount()==null){
                    baseSettlement.setTotalAmount(0d);
            }
            if(baseSettlement.getTotalAdvertAmount()==null){
                baseSettlement.setTotalAdvertAmount(0d);
            }
            Double totalAmount = baseSettlement.getTotalAmount();
            Integer type = baseSettlement.getType();
            String context = baseSettlement.getContext();
            Double count=0.0;
            Double count1=0.0;
            Double totalAdvertAmount = baseSettlement.getTotalAdvertAmount();
            //扣量算法
            if(type==1){
                JSONObject jsonObject = JSONObject.parseObject(context);
                Set<String> keySet = jsonObject.keySet();
                ArrayList<Integer> list1 = new ArrayList<>();
                list1.add(0);
                for (String s : keySet) {
                    list1.add(Integer.valueOf(s));
                }
                Collections.sort(list1);
                int size = list1.size();
                int flag1=0;
                int flag2=0;
                Double a1=0D;
                Double a2=0D;
                for(int i=1;i<size;i++){
                    if(totalAmount>=list1.get(i-1)&&totalAmount<=list1.get(i)){
                        flag1=i;
                        a1=totalAmount-list1.get(i-1);
                        count=a1*jsonObject.getDouble(String.valueOf( list1.get(i) ) );
                        break;
                    }
                }

                for(int i=1;i<size;i++){
                    if(totalAdvertAmount>=list1.get(i-1)&&totalAdvertAmount<=list1.get(i)){
                        flag2=i;
                        a2=totalAdvertAmount-list1.get(i-1);
                        count1=a2*jsonObject.getDouble(String.valueOf( list1.get(i) ) );
                        break;
                    }
                }

                for(int i=1;i<flag1;i++){
                    count+=list1.get(i)*jsonObject.getDouble(String.valueOf( list1.get(i) ));
                }

                for(int i=1;i<flag2;i++){
                    count1+=list1.get(i)*jsonObject.getDouble(String.valueOf( list1.get(i) ));
                }

            }else {

                count=totalAmount*Double.valueOf(context);

                count1=totalAmount*Double.valueOf(context);

            }

            count=Double.valueOf(df.format(count));
            baseSettlement.setTotalAmount(count);
            baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));


            count=Double.valueOf(df.format(count1));
            Integer id = baseSettlement.getCopyrightId();
            if(id==3219||id==3230||id==3254||id==3253){
                count=Double.valueOf(df.format(baseSettlement.getTotalAmount()+count));
                baseSettlement.setTotalAmount(count);
                baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));
                baseSettlement.setTotalAdvertAmount(0d);
                baseSettlement.setSettlementAdAmount(0d);
            }else {
                baseSettlement.setTotalAdvertAmount(count);
                baseSettlement.setSettlementAdAmount(Double.valueOf(df.format(count*0.5)));
            }
            baseSettlement.setCountSettlement(Double.valueOf(df.format(baseSettlement.getSettlementAmount()+baseSettlement.getSettlementAdAmount())));
        }
        Integer count = copyrightDao.selectBookDetailCount(baseSettlementList,bookId,bookName,startTime,endTime);
        return PageTableHandler.getJSONObject(baseSettlements,count);
    }

    @Override
    public String getAllCopyright() {
        List<BaseSettlement> baseSettlements = copyrightDao.selectCopyrightById(UserUtil.getLoginUser().getId());
        Integer roleId = copyrightDao.selectRoleId(UserUtil.getLoginUser().getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",baseSettlements);
        jsonObject.put("roleId",roleId);
        return jsonObject.toJSONString();
    }

    @Override
    public String getSchema(Integer copyrightId) {
        Double schema=0.0;
        Integer roleId = copyrightDao.selectRoleId(UserUtil.getLoginUser().getId());
        Integer status=null;
        List<BaseSettlement> baseSettlementList =new ArrayList<>();
        if(copyrightId==null||copyrightId==0){
            baseSettlementList = copyrightDao.selectCopyrightById(UserUtil.getLoginUser().getId());
        }else {
            BaseSettlement baseSettlement = new BaseSettlement();
            baseSettlement.setCopyrightId(copyrightId);
            baseSettlementList.add(baseSettlement);
        }
        if(roleId!=3){
            status=1;
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectTableListMonthSchema(baseSettlementList,status);

        DecimalFormat df = new DecimalFormat("#.00");
        for (BaseSettlement baseSettlement : baseSettlements) {

            Integer type = baseSettlement.getType();
            Double totalAmount = baseSettlement.getTotalAmount();
            String context = baseSettlement.getContext();
            Double count=0.0;
            Double count1=0.0;
            Double totalAdvertAmount = baseSettlement.getTotalAdvertAmount();
            //扣量算法
            if(type==1){
                JSONObject jsonObject = JSONObject.parseObject(context);
                Set<String> keySet = jsonObject.keySet();
                ArrayList<Integer> list = new ArrayList<>();
                list.add(0);
                for (String s : keySet) {
                    list.add(Integer.valueOf(s));
                }
                Collections.sort(list);
                int size = list.size();
                int flag=0;
                int flag1=0;
                Double a1=0D;
                Double a2=0D;
                for(int i=1;i<size;i++){
                    if(totalAmount>=list.get(i-1)&&totalAmount<=list.get(i)){
                        flag=i;
                        a1=totalAmount-list.get(i-1);
                        count=a1*jsonObject.getDouble(String.valueOf( list.get(i) ) );
                        break;
                    }
                }

                for(int i=1;i<size;i++){
                    if(totalAdvertAmount>=list.get(i-1)&&totalAdvertAmount<=list.get(i)){
                        flag1=i;
                        a2=totalAdvertAmount-list.get(i-1);
                        count1=a2*jsonObject.getDouble(String.valueOf( list.get(i) ) );
                        break;
                    }
                }

                for(int i=1;i<flag;i++){
                    count+=list.get(i)*jsonObject.getDouble(String.valueOf( list.get(i) ));
                }

                for(int i=1;i<flag1;i++){
                    count1+=list.get(i)*jsonObject.getDouble(String.valueOf( list.get(i) ));
                }

            }else {

                count=totalAmount*Double.valueOf(context);

                count1=totalAmount*Double.valueOf(context);

            }

            count=Double.valueOf(df.format(count));
            baseSettlement.setTotalAmount(count);
            baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));

            count=Double.valueOf(df.format(count1));
            Integer id = baseSettlement.getCopyrightId();
            if(id==3219||id==3230||id==3254||id==3253){
                count=Double.valueOf(df.format(baseSettlement.getTotalAmount()+count));
                baseSettlement.setTotalAmount(count);
                baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));
                baseSettlement.setTotalAdvertAmount(0d);
                baseSettlement.setSettlementAdAmount(0d);
            }else {
                baseSettlement.setTotalAdvertAmount(count);
                baseSettlement.setSettlementAdAmount(Double.valueOf(df.format(count*0.5)));
            }
            baseSettlement.setCountSettlement(Double.valueOf(df.format(baseSettlement.getSettlementAmount()+baseSettlement.getSettlementAdAmount())));
            if(baseSettlement.getStatus().equals(0)){
                schema+=Double.valueOf(df.format(baseSettlement.getSettlementAmount()+baseSettlement.getSettlementAdAmount()));
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("schema",Double.valueOf(df.format(schema)));
        return jsonObject.toJSONString();
    }

    @Override
    public String selectBookInfo(Integer copyrightId,Integer page,Integer limit) {
        List<BaseSettlement> baseSettlementList =new ArrayList<>();
        if(copyrightId==null||copyrightId==0){
            baseSettlementList = copyrightDao.selectCopyrightById(UserUtil.getLoginUser().getId());
        }else {
            BaseSettlement baseSettlement = new BaseSettlement();
            baseSettlement.setCopyrightId(copyrightId);
            baseSettlementList.add(baseSettlement);
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectBookInfo(baseSettlementList,page,limit);
        Integer count = copyrightDao.selectBookInfoCount(baseSettlementList);
        return PageTableHandler.getJSONObject(baseSettlements,count);
    }

    public void updateUserCopyrightMap(){
        List<BaseSettlement> baseSettlements = copyrightDao.selectCopyrightAll();
        List<Long> users = copyrightDao.selectUserAll();
        List<BaseSettlement> baseSettlementList=new ArrayList<>();
        for (Long user : users) {
            for (BaseSettlement baseSettlement : baseSettlements) {
                BaseSettlement settlement = new BaseSettlement();
                settlement.setUserId(user);
                settlement.setCopyrightId(baseSettlement.getCopyrightId());
                settlement.setCopyrightName(baseSettlement.getCopyrightName());
                baseSettlementList.add(settlement);
            }
        }
        copyrightDao.updateUserCopyrightMap(baseSettlementList);
    }
}
