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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;

@Service
public class CopyrightBookServiceImpl implements CopyrightBookService {
    @Autowired
    CopyrightDao copyrightDao;
    @Override
    public String selectSettlement(Integer copyrightId,Integer page,Integer limit) {
        if(copyrightId==null||copyrightId==0){
            copyrightId=null;
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectTableListMonth(copyrightId,page-1,limit);
        DecimalFormat df = new DecimalFormat("#.00");
        for (BaseSettlement baseSettlement : baseSettlements) {
            Double totalAmount = baseSettlement.getTotalAmount();
            Double count=0.0;
            if(totalAmount<=2000.0){
                count=totalAmount;
            }else if(totalAmount>=2001.0&&totalAmount<=5000.0){
                Double a1=totalAmount-2000.0;
                count=2000.0+a1*0.5;
            }else if(totalAmount>=5001.0&&totalAmount<=10000.0){
                Double a1=totalAmount-5000.0;
                count=2000.0+3000*0.5+a1*0.2;
            }else if(totalAmount>=10001.0&&totalAmount<=30000.0){
                Double a1=totalAmount-10000.0;
                count=2000.0+3000*0.5+5000*0.2+a1*0.2;
            }else if(totalAmount>=30001.0&&totalAmount<=50000.0){
                Double a1=totalAmount-30000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2+a1*0.15;
            }else if(totalAmount>=50001.0&&totalAmount<=100000.0){
                Double a1=totalAmount-50000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+a1*0.15;
            }else if(totalAmount>=100001.0&&totalAmount<=300000.0){
                Double a1=totalAmount-100000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+a1*0.1;
            }else if(totalAmount>=300001.0&&totalAmount<=500000.0){
                Double a1=totalAmount-300000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1+a1*0.05;
            }else if(totalAmount>=500001.0&&totalAmount<=1000000.0){
                Double a1=totalAmount-500000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+a1*0.02;
            }else if(totalAmount>=1000001.0&&totalAmount<=3000000.0){
                Double a1=totalAmount-1000000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+500000*0.02+a1*0.02;
            }else if(totalAmount>=3000001.0&&totalAmount<=5000000.0){
                Double a1=totalAmount-3000000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+500000*0.02+2000000*0.02+a1*0.02;
            }
            count=Double.valueOf(df.format(count));
            baseSettlement.setTotalAmount(count);
            baseSettlement.setSettlementAmount(Double.valueOf(df.format(count*0.5)));
            count=0.0;
            Double totalAdvertAmount = baseSettlement.getTotalAdvertAmount();
            if(totalAdvertAmount<=2000.0){
                count=totalAdvertAmount;
            }else if(totalAdvertAmount>=2001.0&&totalAdvertAmount<=5000.0){
                Double a1=totalAdvertAmount-2000.0;
                count=2000.0+a1*0.5;
            }else if(totalAdvertAmount>=5001.0&&totalAdvertAmount<=10000.0){
                Double a1=totalAdvertAmount-5000.0;
                count=2000.0+3000*0.5+a1*0.2;
            }else if(totalAdvertAmount>=10001.0&&totalAdvertAmount<=30000.0){
                Double a1=totalAdvertAmount-10000.0;
                count=2000.0+3000*0.5+5000*0.2+a1*0.2;
            }else if(totalAdvertAmount>=30001.0&&totalAdvertAmount<=50000.0){
                Double a1=totalAdvertAmount-30000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2+a1*0.15;
            }else if(totalAdvertAmount>=50001.0&&totalAdvertAmount<=100000.0){
                Double a1=totalAdvertAmount-50000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+a1*0.15;
            }else if(totalAdvertAmount>=100001.0&&totalAdvertAmount<=300000.0){
                Double a1=totalAdvertAmount-100000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+a1*0.1;
            }else if(totalAdvertAmount>=300001.0&&totalAdvertAmount<=500000.0){
                Double a1=totalAdvertAmount-300000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1+a1*0.05;
            }else if(totalAdvertAmount>=500001.0&&totalAdvertAmount<=1000000.0){
                Double a1=totalAdvertAmount-500000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+a1*0.02;
            }else if(totalAdvertAmount>=1000001.0&&totalAdvertAmount<=3000000.0){
                Double a1=totalAdvertAmount-1000000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+500000*0.02+a1*0.02;
            }else if(totalAdvertAmount>=3000001.0&&totalAdvertAmount<=5000000.0){
                Double a1=totalAdvertAmount-3000000.0;
                count=2000.0+3000*0.5+5000*0.2+20000*0.2*2+50000*0.15+200000*0.1*2+500000*0.02+2000000*0.02+a1*0.02;
            }
            count=Double.valueOf(df.format(count));
            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(copyrightId);
        return PageTableHandler.getJSONObject(baseSettlements,count);
    }

    @Override
    public String getAllCopyright() {
        List<BaseSettlement> baseSettlements = copyrightDao.selectCopyrightAll();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",baseSettlements);
        return jsonObject.toJSONString();
    }

    @Override
    public String selectBookInfo(Integer copyrightId,Integer page,Integer limit) {
        if(copyrightId==null||copyrightId==0){
            copyrightId=null;
        }
        List<BaseSettlement> baseSettlements = copyrightDao.selectBookInfo(copyrightId,page,limit);
        Integer count = copyrightDao.selectBookInfoCount(copyrightId);
        return PageTableHandler.getJSONObject(baseSettlements,count);
    }
}
