package com.goafanti.expenseAccount.service.impl;

import cn.jiguang.common.utils.StringUtils;
import com.goafanti.common.dao.OrderYearMaxDurationMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.model.OrderYearMaxDuration;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.expenseAccount.bo.ExpenseCountsBo;
import com.goafanti.expenseAccount.bo.ExpenseCountsInput;
import com.goafanti.expenseAccount.service.ExpenseCountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Service
public class ExpenseCountsServiceImpl implements ExpenseCountsService {

    @Autowired
    private TOrderNewMapper tOrderNewMapper;
    @Autowired
    private OrderYearMaxDurationMapper orderYearMaxDurationMapper;
    @Override
    @Cacheable(value = "getCounts#300", key = "'getCounts:key='+#in.buyerName+#in.startTime+#in.endTime+#in.depId+#in.contactNo+#in.targetType")
    public Object getCounts(ExpenseCountsInput in) {
        if (in.getTargetType()==null)in.setTargetType(1);
        if (in.getTargetType()==1){
            List<ExpenseCountsBo> expenseCountsBos = tOrderNewMapper.selectExpenseCount(in);
            expenseCountsBos.forEach(e->{
                //获取年份
                if (StringUtils.isNotEmpty(e.getOrderNo())){
                    List<OrderYearMaxDuration> yearMaxDurationList = orderYearMaxDurationMapper.selectByOrderNo(e.getOrderNo());
                    int year = LocalDate.now().getYear();
                    OrderYearMaxDuration useOyd = null;
                    for (int i = 0; i < yearMaxDurationList.size(); i++) {
                        OrderYearMaxDuration oyd = yearMaxDurationList.get(i);
                        //年份为0不是会员直接获取跳出循环，否则获取当前年份，获取不到获取最后一年
                        if (oyd.getYear()==0){
                            useOyd=oyd;
                            break;
                        }else {
                            if (oyd.getYear().equals(year)){
                                useOyd = oyd;
                                break;
                            }
                            if(i==yearMaxDurationList.size()-1&&useOyd==null){
                                useOyd = yearMaxDurationList.get(i);
                            }
                        }

                    }
                    if(useOyd !=null&&useOyd.getStatus()==1){
                        e.setMaxDuration("不限制");
                        e.setExceedDuration("0");
                    }else {
                        e.setMaxDuration(useOyd.getMaxDuration().toString());
                        if (useOyd.getMaxDuration().compareTo(new BigDecimal(e.getActualDuration()))>=0){
                            e.setExceedDuration("0");
                        }else {
                            e.setExceedDuration(new BigDecimal(e.getActualDuration()).subtract(useOyd.getMaxDuration()).toString());
                        }
                    }
                }
            });
            return expenseCountsBos;
        }else {
            return tOrderNewMapper.selectExpenseDepCount(in);
        }
    }

    @Override
    @CacheEvict(value = "getCounts#300",allEntries = true)
    public Object getCountsClear() {
        LoggerUtils.debug(getClass(),"报销统计缓存清除。");
        return 1;
    }


}
