package com.sckw.payment.service.operate;

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.payment.dao.KwpSettlementOfflineMapper;
import com.sckw.payment.model.dto.SettlementOfflineDto;
import com.sckw.payment.model.vo.req.SettlementOfflineReq;
import com.sckw.payment.model.vo.res.SettlementOfflineVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.UserCacheResDto;
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 xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SettlementOfflineService {
    private final KwpSettlementOfflineMapper settlementOfflineMapper;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * 分页查询线下结算记录
     *
     * @param settlementOfflineReq
     * @return
     */
    public PageRes<SettlementOfflineVo> list(SettlementOfflineReq settlementOfflineReq) {
        PageHelper.startPage(settlementOfflineReq.getPage(), settlementOfflineReq.getPageSize());
        List<SettlementOfflineDto> settlementOfflineDto = settlementOfflineMapper.list(settlementOfflineReq);
        if (CollectionUtils.isEmpty(settlementOfflineDto)) {
            return new PageRes<>();
        }
        Map<Long, UserCacheResDto> map = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        DecimalFormat df = new DecimalFormat("0.00");
        PageInfo<SettlementOfflineDto> pageInfo = new PageInfo<>(settlementOfflineDto);
        //指定返回值
        List<SettlementOfflineVo> collect = settlementOfflineDto.stream().map(a -> {
            SettlementOfflineVo settlementOfflineVo = new SettlementOfflineVo();
            BeanUtils.copyProperties(a, settlementOfflineVo);

            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)) {
                settlementOfflineVo.setCreateByText(userCacheResDto.getName());
            }
            userCacheResDto = map.get(updateBy);
            if (Objects.isNull(userCacheResDto)) {
                userCacheResDto = remoteSystemService.queryUserCacheById(updateBy);
                map.put(updateBy, userCacheResDto);
            }
            if (Objects.nonNull(userCacheResDto)) {
                settlementOfflineVo.setUpdateByText(userCacheResDto.getName());
            }
            settlementOfflineVo.setPayPrice(df.format(Objects.isNull(a.getPayPrice()) ? new BigDecimal("0.0") : a.getPayPrice()));
            settlementOfflineVo.setTopayPrice(df.format(Objects.isNull(a.getTopayPrice()) ? new BigDecimal("0.0") : a.getTopayPrice()));
            settlementOfflineVo.appendPrefix();
            return settlementOfflineVo;
        }).collect(Collectors.toList());
        return PageRes.build(pageInfo, collect);
    }

}
