package shop.alien.store.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.StoreCashOutRecord;
import shop.alien.entity.store.StoreIncomeDetailsRecord;
import shop.alien.entity.store.vo.StoreCashOutRecordVo;
import shop.alien.mapper.StoreCashOutRecordMapper;
import shop.alien.mapper.StoreIncomeDetailsRecordMapper;
import shop.alien.store.service.StoreCashOutRecordService;
import shop.alien.util.common.ListToPage;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商户提现记录表 服务实现类
 *
 * @author ssk
 * @since 2025-02-25
 */
@Service
@RequiredArgsConstructor
public class StoreCashOutRecordServiceImpl extends ServiceImpl<StoreCashOutRecordMapper, StoreCashOutRecord> implements StoreCashOutRecordService {

    private final StoreCashOutRecordMapper storeCashOutRecordMapper;
    private final StoreIncomeDetailsRecordMapper storeIncomeDetailsRecordMapper;

    @Override
    public JSONObject getCanCashOutMoney(Integer storeId) {
        return null;
    }

    @Override
    public StoreCashOutRecordVo getCashOutRecordList(Integer storeId, String cashOutStartTime, String cashOutEndTime, Integer page, Integer size, String paymentStatus) {
        QueryWrapper<StoreCashOutRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("scor.store_id", storeId);
        wrapper.eq(StringUtils.isNotBlank(paymentStatus), "scor.payment_status", paymentStatus);
        wrapper.le(StringUtils.isNotBlank(cashOutEndTime) , "scor.created_time", cashOutEndTime + " 23:59:59");
        wrapper.ge(StringUtils.isNotBlank(cashOutStartTime), "scor.created_time", cashOutStartTime + " 00:00:00" );
        wrapper.eq("scor.delete_flag", 0);
        wrapper.orderByDesc("scor.created_time");
        List<StoreCashOutRecord> recordList = storeCashOutRecordMapper.selectCashoutRecordList(wrapper);
        IPage<StoreCashOutRecord> storeCashOutRecordIPage = ListToPage.setPage(recordList, page, size);
        StoreCashOutRecordVo vo = new StoreCashOutRecordVo();
        vo.setCashOutRecordList( storeCashOutRecordIPage.getRecords());
        vo.setCashOutAllMoney(new BigDecimal(recordList.stream().filter(item -> "1".equals(item.getPaymentStatus().toString())).mapToInt(StoreCashOutRecord::getMoney).sum()).divide(new BigDecimal(100), 2, RoundingMode.DOWN));
        vo.setCashOutNum(recordList.size());
        return vo;
    }

    @Override
    public StoreCashOutRecordVo getCashOutRecordById(Integer id) {
        StoreCashOutRecordVo vo = new StoreCashOutRecordVo();
        StoreCashOutRecord record = storeCashOutRecordMapper.selectById(id);
        if (null != record) {
            BeanUtils.copyProperties(record, vo);
            LambdaQueryWrapper<StoreIncomeDetailsRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StoreIncomeDetailsRecord::getStoreId, record.getStoreId());
            wrapper.ge(StoreIncomeDetailsRecord::getCreatedTime, record.getIncomeStartTime());
            wrapper.le(StoreIncomeDetailsRecord::getCreatedTime, record.getIncomeEndTime());
            List<StoreIncomeDetailsRecord> incomeDetailsRecordList = storeIncomeDetailsRecordMapper.selectList(wrapper);
            Map<String, Integer> incomeMap = incomeDetailsRecordList.stream().collect(
                    Collectors.groupingBy(
                            item -> item.getCreatedTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().toString(),
                            Collectors.summingInt(StoreIncomeDetailsRecord::getMoney)));
            JSONArray array = new JSONArray();
            incomeMap.forEach((key, value) -> {
                JSONObject object = new JSONObject();
                object.put("date", key);
                object.put("money", new BigDecimal(value).divide(new BigDecimal(100), 0, RoundingMode.HALF_UP));
                array.add(object);
            });
            vo.setIncomeList(array);
        }
        return vo;
    }
}
