package com.pj.project.tb_fee_statistics;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.api.wx.bo.PriceBO;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_business_car.TbBusinessCar;
import com.pj.project.tb_business_car.TbBusinessCarService;
import com.pj.project.tb_business_item.TbBusinessItem;
import com.pj.project.tb_fee_details.TbFeeDetails;
import com.pj.project.tb_fee_details.TbFeeDetailsService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_fee_statistics -- 收费统计表
 * @author lzm 
 */
@Service
@Transactional
public class TbFeeStatisticsService extends ServiceImpl<TbFeeStatisticsMapper, TbFeeStatistics> implements IService<TbFeeStatistics> {

	/** 底层 Mapper 对象 */
	@Autowired
	TbFeeStatisticsMapper tbFeeStatisticsMapper;
	@Resource
    TbBusinessCarService tbBusinessCarService;
	@Resource
    TbBusinessService tbBusinessService;
    @Resource
    TbFeeDetailsService tbFeeDetailsService;

	/** 增 */
	int add(TbFeeStatistics t){
		return tbFeeStatisticsMapper.add(t);
	}

	/** 删 */
	int delete(Long id){
		return tbFeeStatisticsMapper.delete(id);
	}

	/** 改 */
	int update(TbFeeStatistics t){
		return tbFeeStatisticsMapper.update(t);
	}

	/** 查 */
	TbFeeStatistics getById(Long id){
		return tbFeeStatisticsMapper.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	List<TbFeeStatistics> getList(SoMap so) {
        return tbFeeStatisticsMapper.getList(so);
    }

    List<TbFeeStatistics> getMonth(SoMap so) {
        return tbFeeStatisticsMapper.getMonth(so);
    }

    List<TbFeeStatistics> getYear(SoMap so) {
        return tbFeeStatisticsMapper.getYear(so);
    }

    public TbFeeStatistics getByDayAndFeeType(String day, Integer feeType){
//        TbFeeStatistics Fee = tbFeeStatisticsMapper.getByDayAndFeeType(Day, feeType);
        QueryWrapper<TbFeeStatistics> qw = new QueryWrapper<>();
        qw.eq("day", day);
        qw.eq("fee_type", feeType);
        return getOne(qw);
    }

    @Async
    public void countParkFee(List<PriceBO> cars){
        Date now = new Date();
        String toDay = DateUtil.format(now, "yyyy-MM-dd");
        String thisYear = StrUtil.sub(toDay, 0, 4);
        String thisMonth = StrUtil.sub(toDay, 0, 7);
        TbFeeStatistics parkFee = this.getByDayAndFeeType(toDay, TbFeeStatistics.feeType.PARK_FEE.getCode());
        if(parkFee == null){
            parkFee = new TbFeeStatistics();
            parkFee.setTaxRate(new BigDecimal(0.09));
        }
        for (PriceBO bo1 : cars) {
            BigDecimal price = bo1.getP();
            TbBusinessCar car = tbBusinessCarService.getById(bo1.getId());
            if(car.getMoney().compareTo(new BigDecimal(0)) == 0){
                parkFee.setNum(parkFee.getNum() + 1);
            }
            parkFee.setTaxMoney(parkFee.getTaxMoney().add(price));
        }
        BigDecimal noTaxMoney = parkFee.getTaxMoney().subtract(parkFee.getTaxMoney().multiply(parkFee.getTaxRate()));
        parkFee.setNoTaxMoney(noTaxMoney);
        parkFee.setDayTime(now).setMonth(thisMonth).setYear(thisYear).setDay(toDay)
                .setFeeType(TbFeeStatistics.feeType.PARK_FEE.getCode()).setItemTypeName(TbFeeStatistics.feeType.PARK_FEE.getDesc())
                .setPayType(3);
        this.saveOrUpdate(parkFee);
    }

    @Async
    public void countBusinessFee(List<TbBusinessItem> items){
        Date now = new Date();
        String toDay = DateUtil.format(now, "yyyy-MM-dd");
        String thisYear = StrUtil.sub(toDay, 0, 4);
        String thisMonth = StrUtil.sub(toDay, 0, 7);
        for (TbBusinessItem item : items) {
            int feeType = -1;
            String itemType = item.getItemTypeName();
            if(itemType.contains("核酸")){
                feeType = TbFeeStatistics.feeType.NUCLEIC_FEE.getCode();
            } else if(itemType.contains("消毒")){
                feeType = TbFeeStatistics.feeType.DISINFECT_FEE.getCode();
            } else if(itemType.contains("装卸")){
                feeType = TbFeeStatistics.feeType.STEVEDORE_FEE.getCode();
            } else if(StrUtil.equals(itemType, "特殊车辆") || StrUtil.equals(itemType, "充电打冷") || StrUtil.equals(itemType, "汽车吊" )){
                feeType = TbFeeStatistics.feeType.STEVEDORE_FEE.getCode();
            } else if(itemType.contains("入场管理")){
                feeType = TbFeeStatistics.feeType.MANAGE_FEE.getCode();
            }
            TbFeeStatistics businessFee = this.getByDayAndFeeType(toDay, feeType);
            if(businessFee == null){
                businessFee = new TbFeeStatistics();
                businessFee.setTaxRate(new BigDecimal(0.06));
            }
            businessFee.setTaxMoney(businessFee.getTaxMoney().add(item.getItemPrice()));
            businessFee.setNum(businessFee.getNum() + 1);
            BigDecimal noTaxMoney = businessFee.getTaxMoney().subtract(businessFee.getTaxMoney().multiply(businessFee.getTaxRate()));
            businessFee.setNoTaxMoney(noTaxMoney);
            businessFee.setDayTime(now).setMonth(thisMonth).setYear(thisYear).setDay(toDay)
                    .setFeeType(feeType).setItemTypeName(itemType)
                    .setPayType(3);
            this.saveOrUpdate(businessFee);
            /*-----------------分割线------------------*/
            //添加此item的收费明细
            TbBusiness business = tbBusinessService.getById(item.getBusinessId());
            TbFeeDetails businessFeeDetail = tbFeeDetailsService.getByBusinessIdAndCarNoAndItemType(item.getBusinessId(), business.getCardNo(), item.getItemTypeId());
            if(businessFeeDetail == null){
                businessFeeDetail = new TbFeeDetails();
            }
            businessFeeDetail.setBusinessId(item.getBusinessId()).setBusinessNo(business.getNo())
                    .setCarNo(business.getCardNo())
                    .setItemPrice(item.getItemPrice())
                    .setFeeType(feeType)
                    .setItemTypeId(item.getItemTypeId()).setItemTypeName(item.getItemTypeName())
                    .setItemId(item.getItemId()).setItemName(item.getItemName())
                    .setPayDay(toDay).setPayType(3).setCreateTime(now);
            tbFeeDetailsService.saveOrUpdate(businessFeeDetail);
        }
    }


}
