package com.yuchen.saas.park.service.service.property.bill;

import com.yuchen.saas.park.api.dto.property.CreateBillDto;
import com.yuchen.saas.park.api.dto.property.PropertyBusinessCreateBillDto;
import com.yuchen.saas.park.api.entity.property.PropertyBill;
import com.yuchen.saas.park.api.entity.property.PropertyBillSub;
import com.yuchen.saas.park.api.entity.property.PropertyFeeStandard;
import com.yuchen.saas.park.api.enums.property.BillStatusEnum;
import com.yuchen.saas.park.api.enums.property.CreateBillTypeEnum;
import com.yuchen.saas.park.service.event.property.CreateBillSuccessEvent;
import com.yuchen.saas.park.service.mapper.property.PropertyBillMapper;
import com.yuchen.saas.park.service.service.property.PropertyBillSubService;
import com.yuchen.saas.park.service.service.property.PropertyFeeStandardService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BusinessBillServiceImpl extends BasePropertyBillService implements IPropertyBillService {
    @Resource
    private PropertyBillMapper billMapper;
    @Resource
    private PropertyBillSubService propertyBillSubService;
    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;
    @Resource
    private ApplicationContext applicationContext;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBill(CreateBillDto createBillDto) {
        super.validParam(createBillDto);
        List<PropertyBusinessCreateBillDto> billDtoList = createBillDto.getBusinessCreateBillDtoList();
        for (PropertyBusinessCreateBillDto businessBill : billDtoList) {
            PropertyFeeStandard fee = propertyFeeStandardService.getById(businessBill.getFeeId());
            PropertyBill bill = this.assembleBill(fee,businessBill);
            billMapper.insert(bill);

            PropertyBillSub sub = super.assembleBillSub(fee);
            sub.setBillId(bill.getId());
            propertyBillSubService.save(sub);

            applicationContext.publishEvent(new CreateBillSuccessEvent(bill.getId(),createBillDto.getCreateBillTypeEnum()));
        }
    }

    /**
     * 账单信息组装
     */
    private PropertyBill assembleBill(PropertyFeeStandard fee, PropertyBusinessCreateBillDto businessBill) {
        PropertyBill bill = super.assembleBaseBill(fee, CreateBillTypeEnum.BUSINESS);
        Date startDate = businessBill.getStartDate();
        Date endDate = businessBill.getEndDate();
        long receivableAmt = businessBill.getReceivableAmtFen();
        Date receivableDate = businessBill.getReceivableDate();

        bill.setBillStatus(BillStatusEnum.YES_OUT.getValue()); // 临时收费直接出账
        bill.setStartDate(startDate);
        bill.setEndDate(endDate);
        bill.setReceivableAmtFen(super.decimalPoint(fee.getParkId(),receivableAmt));
        bill.setReceivableDate(receivableDate);
        bill.setBreachDate(super.getBreachDate(receivableDate, startDate, endDate, fee));
        bill.setTaxAmtFen(super.calcTaxAmtFen(fee.getFeeItemId(),bill.getReceivableAmtFen()));
        bill.setCustomerId(businessBill.getCustomerId());
        bill.setRetraceId(businessBill.getBusinessChargesId());
        bill.setOutBillTime(new Date());

        return bill;
    }

}
