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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuchen.saas.park.api.dto.property.CreateBillDto;
import com.yuchen.saas.park.api.dto.property.PropertyShareFeeCreateBillDto;
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.entity.property.PropertyHouses;
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 com.yuchen.saas.park.service.service.property.PropertyHousesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 固定分摊金额账单
 */
@Slf4j
@Service
public class ShareFeeBillServiceImpl extends BasePropertyBillService implements IPropertyBillService {
    @Resource
    private PropertyBillMapper billMapper;
    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;
    @Resource
    private PropertyBillSubService propertyBillSubService;
    @Resource
    private PropertyHousesService propertyHousesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBill(CreateBillDto createBillDto) {
        PropertyShareFeeCreateBillDto billDto = createBillDto.getShareFeeCreateBillDto();
        super.validParam(createBillDto);
        if (!this.verifyBiz(billDto)) {
            return;
        }
        PropertyFeeStandard fee = propertyFeeStandardService.getById(billDto.getFeeId());
        PropertyBill bill = this.assembleBill(createBillDto,fee);
        billMapper.insert(bill);

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


    /**
     * 账单信息组装
     *
     * @param dto
     * @return
     */
    private PropertyBill assembleBill(CreateBillDto dto, PropertyFeeStandard fee) {
        PropertyBill bill = super.assembleBaseBill(fee, dto.getCreateBillTypeEnum());
        PropertyShareFeeCreateBillDto billDto = dto.getShareFeeCreateBillDto();
        Date startDate = billDto.getStartDate();
        Date endDate = billDto.getEndDate();
        long receivableAmt = billDto.getShareAmtFen();
        Date receivableDate = super.getReceivableDate(startDate, endDate, fee);

        PropertyHouses houses = propertyHousesService.getById(billDto.getHouseId());
        bill.setBuildingId(houses.getBuildingId());
        bill.setHouseId(billDto.getHouseId());
        bill.setCustomerId(super.getHouseCustomerId(billDto.getHouseId()));
        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.setRetraceId(billDto.getShareFeeId());

        bill.setTaxAmtFen(super.calcTaxAmtFen(fee.getFeeItemId(),bill.getReceivableAmtFen()));

        return bill;
    }


    /**
     * 业务校验
     * @param billDto
     * @return
     */
    private boolean verifyBiz(PropertyShareFeeCreateBillDto billDto) {
        LambdaQueryWrapper<PropertyBill> qw = new LambdaQueryWrapper<>();
        qw.eq(PropertyBill::getHouseId,billDto.getHouseId());
        qw.eq(PropertyBill::getRetraceId,billDto.getShareFeeId());
        PropertyBill bill = billMapper.selectOne(qw);
        if (bill != null) {
            return false;
        }
        return true;
    }



}
