package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.domain.dto.HdZdDTO;
import com.ruoyi.web.domain.dto.GrzdDTO;
import com.ruoyi.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.ZdInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 账单信息Service业务层处理
 * 
 * @author shenzx
 * @date 2025-09-17
 */
@Service
public class ZdInfoServiceImpl extends ServiceImpl<ZdInfoMapper, ZdInfo> implements IZdInfoService {

    private IHdZdService hdZdService;
    @Autowired
    public void setHdZdService(IHdZdService hdZdService) {
        this.hdZdService = hdZdService;
    }

    private IHdQdService hdQdService;
    @Autowired
    public void setHdQdService(IHdQdService hdQdService) {
        this.hdQdService = hdQdService;
    }

    private IZdUserService zdUserService;
    @Autowired
    public void setZdUserService(IZdUserService zdUserService) {
        this.zdUserService = zdUserService;
    }

    private IDealInfoService dealInfoService;
    @Autowired
    public void setDealInfoService(IDealInfoService dealInfoService) {
        this.dealInfoService = dealInfoService;
    }

    private IHdInfoService hdInfoService;
    @Autowired
    public void setHdInfoService(IHdInfoService hdInfoService) {
        this.hdInfoService = hdInfoService;
    }

    @Override
    public void gen() {
        List<HdZdDTO> list = this.baseMapper.selectHdZdList();
        if (list.isEmpty()) return;
        BigDecimal reduce = list.stream().map(HdZdDTO::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        ZdInfo zdInfo = new ZdInfo();
        zdInfo.setMoney(reduce);
        zdInfo.setZdrq(DateUtils.getNowDate());
        this.save(zdInfo);

        Set<Long> hdIdSet = list.stream().map(HdZdDTO::getId).collect(Collectors.toSet());
        List<HdZd> hdZdList = new ArrayList<>();
        hdIdSet.forEach(v->{
            HdZd hdZd = new HdZd();
            hdZd.setHdId(v);
            hdZd.setZdId(zdInfo.getId());
            hdZdList.add(hdZd);
        });
        hdZdService.saveBatch(hdZdList);

        List<ZdUser> zdUserList = new ArrayList<>();
        QueryWrapper<HdQd> hqQW = new QueryWrapper<>();
        hqQW.lambda().select(HdQd::getUserId).in(HdQd::getFid,hdIdSet).groupBy(HdQd::getUserId);
        hdQdService.list(hqQW).stream().map(HdQd::getUserId).forEach(v->{
            ZdUser zdUser = new ZdUser();
            zdUser.setUserId(v);
            zdUser.setZdId(zdInfo.getId());
            zdUserList.add(zdUser);
        });
        zdUserService.saveBatch(zdUserList);
    }

    @Override
    public List<GrzdDTO> grzd() {
        QueryWrapper<ZdUser> zuQW = new QueryWrapper<>();
        zuQW.lambda().eq(ZdUser::getUserId, SecurityUtils.getUserId());
        List<ZdUser> zdUserList = zdUserService.list(zuQW);

        Set<Long> dealIdSet = zdUserList.stream().map(ZdUser::getDealId).collect(Collectors.toSet());
        if (dealIdSet.isEmpty()) return Collections.emptyList();
        List<DealInfo> dealInfoList = dealInfoService.listByIds(dealIdSet);
        Map<Long, String> dealMap = dealInfoList.stream().collect(Collectors.toMap(DealInfo::getId, DealInfo::getJyzt));
        Map<Long, ZdUser> map = zdUserList.stream().collect(Collectors.toMap(ZdUser::getZdId, v->v));

        Set<Long> zdIdSet = zdUserList.stream().map(ZdUser::getZdId).collect(Collectors.toSet());
        if (zdIdSet.isEmpty()) return Collections.emptyList();
        QueryWrapper<ZdInfo> ziQW = new QueryWrapper<>();
        ziQW.lambda().in(ZdInfo::getId,zdIdSet).orderByDesc(ZdInfo::getZdrq).orderByDesc(ZdInfo::getId);
        List<ZdInfo> zdInfoList = this.list(ziQW);
        List<GrzdDTO> grzdDTOList = new LinkedList<>();
        zdInfoList.forEach(v->{
            Long zdId = v.getId();
            GrzdDTO grzdDTO = new GrzdDTO();
            grzdDTO.setId(zdId);
            grzdDTO.setZdrq(v.getZdrq());
            ZdUser zdUser = map.get(zdId);
            if(Objects.isNull(zdUser.getDealId())) grzdDTO.setZfzt("0");
            else grzdDTO.setZfzt(dealMap.get(zdUser.getDealId()));
            grzdDTO.setMoney(v.getMoney());
            grzdDTOList.add(grzdDTO);
        });
        return grzdDTOList;
    }

    @Override
    public void updateZfzt(Long id) {
        QueryWrapper<ZdUser> zuQW = new QueryWrapper<>();
        zuQW.lambda().eq(ZdUser::getUserId, SecurityUtils.getUserId()).eq(ZdUser::getZdId,id);
        ZdUser zdUser = zdUserService.getOne(zuQW);
        DealInfo dealInfo = dealInfoService.getById(zdUser.getDealId());
        dealInfo.setJyzt("2");
        dealInfoService.updateById(dealInfo);
    }

    @Override
    public void updateDealId(ZdUser dto) {
        QueryWrapper<ZdUser> zuQW = new QueryWrapper<>();
        zuQW.lambda().eq(ZdUser::getZdId,dto.getZdId()).eq(ZdUser::getUserId,SecurityUtils.getUserId());
        ZdUser zdUser = zdUserService.getOne(zuQW);
        zdUser.setDealId(dto.getDealId());
        zdUserService.updateById(zdUser);
    }

    @Override
    public List<HdInfo> grzdHd(Long id) {
        QueryWrapper<HdZd> hzQW = new QueryWrapper<>();
        hzQW.lambda().eq(HdZd::getZdId,id);
        List<HdZd> hdZdList = hdZdService.list(hzQW);
        if (hdZdList.isEmpty()) return Collections.emptyList();
        Set<Long> hdIdSet = hdZdList.stream().map(HdZd::getHdId).collect(Collectors.toSet());
        return hdInfoService.listByIds(hdIdSet);
    }
}
