package common.service.impl;

import com.google.api.ads.adwords.lib.AdWordsProductFrameworkModule;
import common.model.*;
import common.repository.*;
import common.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tkio.model.PackageType;
import tkio.repository.PackageTypeRepository;
import util.DateUtil;
import util.ValidateUtil;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mxq on 2018/1/16.
 */
@Service
public class ContractServiceImpl implements ContractService{

    @Autowired
    ContractRepository contractRepository;
    @Autowired
    ContractBodyRepository contractBodyRepository;
    @Autowired
    Account4WebRepository account4WebRepository;
    @Autowired
    PackageTypeRepository packageTypeRepository;
    @Autowired
    PriceLevelMoneyRepository priceLevelMoneyRepository;
    @Autowired
    UserRepository userRepository;

    @Override
    public Map<String, Object> checkAccount(String email) {
        Map<String, Object> map = new HashMap<>();
        Account4Web account4Web = account4WebRepository.findByEmail(email);
        if(null == account4Web){
            map.put("result", 0);
        }else{
            BigInteger integer = contractRepository.countNumByDs(DateUtil.getBeforeDays(0));
            map.put("result", 1);
            map.put("company", account4Web.getCompany());
            map.put("num", integer.intValue());
        }
        return map;
    }

    @Override
    public List<ContractBody> findBody() {
        return contractBodyRepository.findAll();
    }

    @Override
    public String getContractCode(String code) {
        BigInteger integer = contractRepository.countNumByDs(DateUtil.getBeforeDays(0));
        String lastCode = "";
        if(integer.intValue() < 9){
            lastCode = "0" + (integer.intValue() + 1);
        }else{
            lastCode = "" + (integer.intValue() + 1);
        }
        String re = code + "-" + DateUtil.getDays2(0) + lastCode;
        return re;
    }

    @Override
    public List<PackageType> findSetmeal() {
        return packageTypeRepository.findAll();
    }

    @Override
    public Double getRebat(String product, Integer level, Long money) {
        PriceLevelMoney levelMoney = priceLevelMoneyRepository.getOne(product, level);
        return money * 1.0  / levelMoney.getMoney();
    }

    @Override
    public List<User> getSales() {
        return userRepository.findSales();
    }

    @Override
    public Contract create(User loginUser, Contract resource) {
        if(DateUtil.getBeforeDays(0).compareTo(resource.getStartDate()) > 0){
            resource.setDs(DateUtil.getBeforeDays(0));
            resource.setStatus("wait");
        }else{
            resource.setDs(DateUtil.getBeforeDays(0));
            resource.setStatus("executing");
        }

        return contractRepository.save(resource);
    }

    @Override
    public Contract update(User loginUser, Contract resource) {
        return null;
    }

    @Override
    public List<Contract> findAll(String ds) {
        List<Contract> result = new ArrayList<>();
        List<User> sales = userRepository.findAllSales();
        Map<Long, String> saleMap = new HashMap<>();
        if(ValidateUtil.isValid(sales)){
            for(User s: sales){
                saleMap.put(s.getId(), s.getName());
            }
        }
        List<PackageType> typeList = packageTypeRepository.findAll();
        Map<Long, String> typeMap = new HashMap<>();
        if(ValidateUtil.isValid(typeList)){
            for(PackageType s: typeList){
                typeMap.put(s.getId(), s.getPackageName());
            }
        }
        List<Contract> contractList = contractRepository.findByDs(ds);
        if(ValidateUtil.isValid(contractList)){
            for(Contract c : contractList){
                c.setSaleName(saleMap.containsKey(c.getSale()) ? saleMap.get(c.getSale()) : "");
                c.setPriceLevelName(typeMap.containsKey(c.getPriceLevel()) ? typeMap.get(c.getPriceLevel()) : "");
                result.add(c);
            }
        }
        return result;
    }
}
