package com.jeesharp.business.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.food.entity.FoodCompanySubsidyUse;
import com.jeesharp.business.modules.food.mapper.FoodCompanySubsidyUseMapper;
import com.jeesharp.business.modules.food.service.FoodCompanySubsidyUseService;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.query.PaginationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class FoodCompanySubsidyUseServiceImpl extends CrudService<FoodCompanySubsidyUseMapper, FoodCompanySubsidyUse, PaginationVO> implements FoodCompanySubsidyUseService {

    private static final String DEL_NORMAL = "0";
    private static final int SUBSIDY_STATUS_FROZEN = 0;
    private static final int SUBSIDY_STATUS_USED = 1;
    private static final BigDecimal ZERO = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);

    @Override
    public BigDecimal sumUsedAmount(String employeeId, String companyId, LocalDate mealDate, String mealType) {
        LambdaQueryWrapper<FoodCompanySubsidyUse> wrapper = new LambdaQueryWrapper<FoodCompanySubsidyUse>()
            .eq(FoodCompanySubsidyUse::getCompanyId, companyId)
            .eq(FoodCompanySubsidyUse::getEmployeeId, employeeId)
            .eq(FoodCompanySubsidyUse::getMealDate, mealDate)
            .in(FoodCompanySubsidyUse::getStatus, SUBSIDY_STATUS_FROZEN, SUBSIDY_STATUS_USED)
            .eq(FoodCompanySubsidyUse::getDelFlag, DEL_NORMAL);
        if (StrUtil.isNotBlank(mealType)) {
            wrapper.eq(FoodCompanySubsidyUse::getMealType, mealType.trim());
        }
        List<FoodCompanySubsidyUse> uses = getBaseMapper().selectList(wrapper);
        return money(uses.stream()
            .map(FoodCompanySubsidyUse::getSubsidyAmount)
            .filter(amount -> amount != null)
            .reduce(BigDecimal.ZERO, BigDecimal::add));
    }

    @Override
    public FoodCompanySubsidyUse getFrozenByOrderId(String orderId) {
        return getBaseMapper().selectOne(new LambdaQueryWrapper<FoodCompanySubsidyUse>()
            .eq(FoodCompanySubsidyUse::getOrderId, orderId)
            .eq(FoodCompanySubsidyUse::getStatus, SUBSIDY_STATUS_FROZEN)
            .eq(FoodCompanySubsidyUse::getDelFlag, DEL_NORMAL)
            .last("limit 1"));
    }

    @Override
    public FoodCompanySubsidyUse getByOrderId(String orderId) {
        return getBaseMapper().selectOne(new LambdaQueryWrapper<FoodCompanySubsidyUse>()
            .eq(FoodCompanySubsidyUse::getOrderId, orderId)
            .eq(FoodCompanySubsidyUse::getDelFlag, DEL_NORMAL)
            .last("limit 1"));
    }

    @Override
    @Transactional(readOnly = false)
    public void insertSubsidyUse(FoodCompanySubsidyUse use) {
        use.setIsNewRecord(true);
        use.preInsert();
        getBaseMapper().insert(use);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSubsidyUse(FoodCompanySubsidyUse use) {
        use.preUpdate();
        getBaseMapper().updateById(use);
    }

    private BigDecimal money(BigDecimal amount) {
        if (amount == null) {
            return ZERO;
        }
        return amount.setScale(2, RoundingMode.HALF_UP);
    }
}
