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.PropertyTempBillCreateDto;
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.mapper.property.PropertyBillMapper;
import com.yuchen.saas.park.service.service.property.PropertyBillSubService;
import com.yuchen.saas.park.service.service.property.PropertyFeeStandardService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class TemporaryBillServiceImpl extends BasePropertyBillService implements IPropertyBillService {
    @Resource
    private PropertyBillMapper billMapper;
    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;
    @Resource
    private PropertyBillSubService propertyBillSubService;

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

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

    }


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

        bill.setBuildingId(tempBill.getBuildingId());
        bill.setHouseId(tempBill.getHouseId());
        bill.setCustomerId(super.getHouseCustomerId(tempBill.getHouseId()));
        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(tempBill.getCustomerId());

        if (Func.isNotEmpty(tempBill.getTemporaryChargesId())) {
            bill.setRetraceId(tempBill.getTemporaryChargesId());
        }

        return bill;
    }



}
