package com.pj.project.tb_fee_details;

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.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.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_details -- 收费明细表
 * @author lzm 
 */
@Service
@Transactional
public class TbFeeDetailsService extends ServiceImpl<TbFeeDetailsMapper, TbFeeDetails> implements IService<TbFeeDetails> {

	/** 底层 Mapper 对象 */
	@Autowired
	TbFeeDetailsMapper tbFeeDetailsMapper;
	@Resource
    TbBusinessCarService tbBusinessCarService;
    @Resource
    TbBusinessService tbBusinessService;

	/** 增 */
	int add(TbFeeDetails t){
		return tbFeeDetailsMapper.add(t);
	}

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

	/** 改 */
	int update(TbFeeDetails t){
		return tbFeeDetailsMapper.update(t);
	}

	/** 查 */
	TbFeeDetails getById(Long id){
		return tbFeeDetailsMapper.getById(id);
	}

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

    public TbFeeDetails getByBusinessIdAndCarNoAndFeeType(String businessId, String carNo, Integer feeType){
        QueryWrapper<TbFeeDetails> qw = new QueryWrapper<>();
        qw.eq("business_id", businessId);
        qw.eq("car_no", carNo);
        qw.eq("fee_type", feeType);
        return getOne(qw);
    }

    public TbFeeDetails getByBusinessIdAndCarNoAndItemType(String businessId, String carNo, String itemTypeId){
        QueryWrapper<TbFeeDetails> qw = new QueryWrapper<>();
        qw.eq("business_id", businessId);
        qw.eq("car_no", carNo);
        qw.eq("item_type_id", itemTypeId);
        return getOne(qw);
    }


	public void chargeParkFee(List<PriceBO> cars){
        Date now = new Date();
        String toDay = DateUtil.format(now, "yyyy-MM-dd");
        for (PriceBO bo1 : cars) {
            TbBusinessCar car = tbBusinessCarService.getById(bo1.getId());
            TbBusiness business = tbBusinessService.getById(car.getBusinessId());
            String carNo = car.getCarNo();
            TbFeeDetails parkFee = getByBusinessIdAndCarNoAndFeeType(car.getBusinessId(), carNo, TbFeeDetails.fee.PARK_FEE.getCode());
            if(parkFee == null){
                parkFee = new TbFeeDetails();
            }
            parkFee.setBusinessId(car.getBusinessId()).setBusinessNo(business.getNo())
                    .setCarNo(car.getCarNo())
                    .setItemPrice(car.getMoney())
                    .setFeeType(TbFeeDetails.fee.PARK_FEE.getCode()).setItemTypeName(TbFeeDetails.fee.PARK_FEE.getDesc())
                    .setPayDay(toDay).setPayType(3).setCreateTime(now);
            saveOrUpdate(parkFee);
        }
    }

    @Async
    public void chargeBusinessFee(List<TbBusinessItem> items){
        Date now = new Date();
        String toDay = DateUtil.format(now, "yyyy-MM-dd");
        for (TbBusinessItem item : items) {
            int feeType = -1;
            String itemType = item.getItemTypeName();
            if(itemType.contains("核酸")){
                feeType = TbFeeDetails.fee.NUCLEIC_FEE.getCode();
            } else if(itemType.contains("消毒")){
                feeType = TbFeeDetails.fee.DISINFECT_FEE.getCode();
            } else if(itemType.contains("装卸")){
                feeType = TbFeeDetails.fee.STEVEDORE_FEE.getCode();
            } else if(StrUtil.equals(itemType, "特殊车辆") || StrUtil.equals(itemType, "充电打冷") || StrUtil.equals(itemType, "汽车吊" )){
                feeType = TbFeeDetails.fee.STEVEDORE_FEE.getCode();
            } else if(itemType.contains("入场管理")){
                feeType = TbFeeDetails.fee.MANAGE_FEE.getCode();
            }
            TbBusiness business = tbBusinessService.getById(item.getBusinessId());
            TbFeeDetails businessFee = getByBusinessIdAndCarNoAndItemType(item.getBusinessId(), business.getCardNo(), item.getItemTypeId());
            if(businessFee == null){
                businessFee = new TbFeeDetails();
            }
            businessFee.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);
            saveOrUpdate(businessFee);
        }
    }

}
