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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuchen.saas.park.api.dto.property.PropertyShareFeeDetailSaveDto;
import com.yuchen.saas.park.api.entity.property.PropertyShareFee;
import com.yuchen.saas.park.api.entity.property.PropertyShareFeeDetail;
import com.yuchen.saas.park.api.enums.property.ShareTypeEnum;
import com.yuchen.saas.park.api.enums.property.YesOrNoEnum;
import com.yuchen.saas.park.api.vo.property.PropertyHousesListVo;
import com.yuchen.saas.park.api.vo.property.PropertyShareFeeDetailListVo;
import com.yuchen.saas.park.service.mapper.property.PropertyShareFeeDetailMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class PropertyShareFeeDetailService extends ServiceImpl<PropertyShareFeeDetailMapper, PropertyShareFeeDetail> {
    @Lazy
    @Resource
    private PropertyShareFeeService propertyShareFeeService;
    @Resource
    private PropertyHousesService propertyHousesService;

    /**
     * 分摊明细保存
     * @param detailSaveDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShareFeeDetail(PropertyShareFeeDetailSaveDto detailSaveDto) {
        LambdaQueryWrapper<PropertyShareFeeDetail> rw = new LambdaQueryWrapper<>();
        rw.eq(PropertyShareFeeDetail::getShareFeeId,detailSaveDto.getShareFeeId());
        this.remove(rw);

        List<String> houseIdList = Arrays.asList(detailSaveDto.getStrHouseIds().split(","));
        List<PropertyShareFeeDetail> detailList = new ArrayList<>();
        houseIdList.forEach(houseId -> {
            PropertyShareFeeDetail detail = new PropertyShareFeeDetail();
            detail.setShareFeeId(detailSaveDto.getShareFeeId());
            detail.setParkId(detailSaveDto.getParkId());
            detail.setHouseId(Long.valueOf(houseId));
            detail.setCreateTime(new Date());
            detailList.add(detail);
        });
        this.saveBatch(detailList,1000);
    }

    /**
     * 分摊明细
     * @param shareFeeId
     * @param current
     * @param size
     * @return
     */
    public Page<PropertyShareFeeDetailListVo> selectPage(String shareFeeId,int current,int size) {
        PropertyShareFee shareFee = propertyShareFeeService.getById(shareFeeId);
        Page<PropertyShareFeeDetailListVo> page = new Page<>(current, size);
        this.baseMapper.selectDetailPage(page,shareFeeId);
        List<Long> houseIdList = page.getRecords().stream()
                .map(PropertyShareFeeDetailListVo::getHouseId).collect(Collectors.toList());
        List<PropertyHousesListVo> houseInfoList = propertyHousesService.queryHouseInfoByHouseIdList(houseIdList);
        page.getRecords().forEach(item -> {
            item.setShareType(shareFee.getShareType());
            houseInfoList.forEach(house -> {
                if (item.getHouseId().equals(house.getId())) {
                    item.setCustomerName(house.getCustomerName());
                }
            });
        });

        if (shareFee.getStatus() == YesOrNoEnum.NO.getValue()) { // 待分摊时，分摊金额未写入数据库需重新计算
            if (shareFee.getShareType() == ShareTypeEnum.BY_USER.getValue()) {
                long shareAmtFen = propertyShareFeeService.getShareAmtFenByUser(Long.parseLong(shareFeeId));
                page.getRecords().forEach(item -> {
                    item.setShareAmtFen(shareAmtFen);
                });
            } else {
                List<PropertyShareFeeDetail> detailList = this.listAreaDetail(Long.parseLong(shareFeeId));
                propertyShareFeeService.setShareAmtFenByArea(Long.parseLong(shareFeeId),detailList);
                page.getRecords().forEach(item -> {
                    detailList.forEach(detail -> {
                        if (item.getId().equals(detail.getId())) {
                            item.setShareAmtFen(detail.getShareAmtFen());
                        }
                    });
                });
            }
        }

        return page;
    }

    /**
     * 查明细带建筑面积
     * @param shareFeeId
     * @return
     */
    public List<PropertyShareFeeDetail> listAreaDetail(long shareFeeId) {
        return this.baseMapper.listAreaDetail(shareFeeId);
    }


    /**
     * 获取分摊总面积
     * @param shareFeeId
     * @return
     */
    public BigDecimal getShareFeeTotalArea(long shareFeeId) {
        return this.baseMapper.getTotalArea(shareFeeId);
    }

}
