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.PropertyMeterCreateBillDto;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 抄表账单
 */
@Service
public class MeterBillServiceImpl extends BasePropertyBillService implements IPropertyBillService {
    @Resource
    private PropertyBillMapper billMapper;
    @Resource
    private PropertyBillSubService propertyBillSubService;
    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;

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

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

    }


    /**
     * 账单信息组装
     */
    private PropertyBill assembleBill(PropertyFeeStandard fee, PropertyMeterCreateBillDto billDto) {
        PropertyBill bill = super.assembleBaseBill(fee, CreateBillTypeEnum.METER);
        Date startDate = billDto.getStartDate();
        Date endDate = billDto.getEndDate();
        long priceFen = fee.getPriceFen() == null ? 0L : fee.getPriceFen();
        BigDecimal receivableAmtBigDecimal = billDto.getBillableQuantity().multiply(new BigDecimal(priceFen)).setScale(0, RoundingMode.HALF_UP);
        long receivableAmt = Long.parseLong(receivableAmtBigDecimal.toString());
        Date receivableDate = super.getReceivableDate(startDate, endDate, fee);

        bill.setBuildingId(billDto.getBuildingId());
        bill.setHouseId(billDto.getHouseId());
        bill.setBillStatus(BillStatusEnum.NO_OUT.getValue());
        bill.setCustomerId(super.getHouseCustomerId(billDto.getHouseId()));
        bill.setReceivableAmtFen(super.decimalPoint(fee.getParkId(),receivableAmt));
        bill.setBillableQuantity(billDto.getBillableQuantity());
        bill.setStartDate(startDate);
        bill.setEndDate(endDate);
        bill.setReceivableDate(receivableDate);
        bill.setMeterFullName(billDto.getMeterFullName());
        bill.setLastMeterReadingNum(billDto.getLastMeterReadingNum());
        bill.setMeterReadingNum(billDto.getMeterReadingNum());
        bill.setMeterUnit(billDto.getMeterUnit());
        bill.setBreachDate(super.getBreachDate(receivableDate, startDate, endDate, fee));
        bill.setTaxAmtFen(super.calcTaxAmtFen(fee.getFeeItemId(),bill.getReceivableAmtFen()));
        bill.setRetraceId(billDto.getMeterReadingId());

        return bill;
    }

}
