package com.sckw.payment.service.operateService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageRes;
import com.sckw.order.api.dubbo.TradeOrderInfoService;
import com.sckw.payment.dao.KwpSettlementLogisticsTrackMapper;
import com.sckw.payment.dao.KwpSettlementWalletMapper;
import com.sckw.payment.model.dto.SettlementWalletDto;
import com.sckw.payment.model.vo.req.SettlementWalletReq;
import com.sckw.payment.model.vo.res.SettlementWalletVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.UserCacheResDto;
import com.sckw.transport.api.dubbo.TransportRemoteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Aick Spt
 * @date 2023-07-20 14:55
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SettlementWalletService {
    private final KwpSettlementWalletMapper settlementWalletMapper;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private TradeOrderInfoService tradeOrderInfoService;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private TransportRemoteService transportRemoteService;
    private final KwpSettlementLogisticsTrackMapper kwpSettlementLogisticsTrackMapper;


    /**
     * 预付款、货到付款记录
     * 前端接口分开
     *
     * @param settlementWalletReq 请求参数
     * @return
     */
    public PageRes<SettlementWalletVo> pageList(SettlementWalletReq settlementWalletReq) {
        PageHelper.startPage(settlementWalletReq.getPage(), settlementWalletReq.getPageSize());
        List<SettlementWalletDto> settlementWalletDto = settlementWalletMapper.list(settlementWalletReq);

        if (CollectionUtils.isEmpty(settlementWalletDto)) {
            return new PageRes<>();
        }
        //增加对应创建人和更新人名称
        Map<Long, UserCacheResDto> map = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        PageInfo<SettlementWalletDto> pageInfo = new PageInfo<>(settlementWalletDto);
        DecimalFormat df = new DecimalFormat("0.00");
        //指定返回值
        List<SettlementWalletVo> collect = settlementWalletDto.stream().map(a -> {
            SettlementWalletVo settlementWalletVo = new SettlementWalletVo();
            BeanUtils.copyProperties(a, settlementWalletVo);
            Long createBy = a.getCreateBy();
            Long updateBy = a.getUpdateBy();
            UserCacheResDto userCacheResDto = map.get(createBy);
            if (Objects.isNull(userCacheResDto)) {
                userCacheResDto = remoteSystemService.queryUserCacheById(createBy);
                map.put(createBy, userCacheResDto);
            }
            if (Objects.nonNull(userCacheResDto)) {
                settlementWalletVo.setCreateByText(userCacheResDto.getName());
            }
            userCacheResDto = map.get(updateBy);
            if (Objects.isNull(userCacheResDto)) {
                userCacheResDto = remoteSystemService.queryUserCacheById(updateBy);
                map.put(updateBy, userCacheResDto);
            }
            if (Objects.nonNull(userCacheResDto)) {
                settlementWalletVo.setUpdateByText(userCacheResDto.getName());
            }
            settlementWalletVo.setPayPrice(df.format(Objects.isNull(a.getPayPrice()) ? new BigDecimal("0.0") : a.getPayPrice()));
            settlementWalletVo.setTopayPrice(df.format(Objects.isNull(a.getTopayPrice()) ? new BigDecimal("0.0") : a.getTopayPrice()));
            return settlementWalletVo;
        }).collect(Collectors.toList());
        return PageRes.build(pageInfo, collect);
    }
}

