package com.tofly.feesapi.bmgl.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bmgl.entity.BmCbb;
import com.tofly.feesapi.bmgl.entity.BmCbpq;
import com.tofly.feesapi.bmgl.entity.CbbConfig;
import com.tofly.feesapi.bmgl.entity.dto.CbbConfigDto;
import com.tofly.feesapi.bmgl.mapper.CbbConfigMapper;
import com.tofly.feesapi.bmgl.service.BmCbbService;
import com.tofly.feesapi.bmgl.service.BmCbpqService;
import com.tofly.feesapi.bmgl.service.CbbConfigService;
import com.tofly.feesapi.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service("cbbConfigService")
public class CbbConfigServiceImpl extends ServiceImpl<CbbConfigMapper, CbbConfig> implements CbbConfigService {
    @Autowired
     private BmCbbService bmCbbService;

    @Autowired
    private BmCbpqService bmCbpqService;

    @Override
    @Transactional
    public boolean saveConfig(CbbConfigDto userCbbConfigDto) {
        this.remove(Wrappers.<CbbConfig>lambdaQuery()
                .eq(CbbConfig::getUserId,userCbbConfigDto.getUserId()));
  if(StringUtil.isNotBlank(userCbbConfigDto.getCbbList())){
      List<CbbConfig> list=new ArrayList<>();
      Arrays.asList(userCbbConfigDto.getCbbList().split(",")).forEach(cbbbm->{
          CbbConfig userCbbConfig=new CbbConfig();
          userCbbConfig.setUserId(userCbbConfigDto.getUserId());
          userCbbConfig.setCbbbm(cbbbm);
          list.add(userCbbConfig);

      });
      this.saveBatch(list);
  }
        return true;
    }

    @Override
    public List<BmCbb> getByCbbByUserId(Long userId) {
         List<String> listBm=getConfigUserId(userId);
      return  bmCbbService.list(Wrappers.<BmCbb>lambdaQuery()
                .eq(BmCbb::getIsdisabled,"1")
                .in(BmCbb::getCbbbm,listBm));
    }
    @Override
    public List<String> getConfigUserId(Long userId){
        List<String> listResult=new ArrayList<>();
        List<CbbConfig> list= this.list(Wrappers.<CbbConfig>lambdaQuery()
                 .eq(CbbConfig::getUserId,userId));
        if(list!=null && list.size()>0){
            listResult= list.stream().map(CbbConfig::getCbbbm).collect(Collectors.toList());
        }else{
            listResult.add("-1000");
        }
        return listResult;
    }

    @Override
    public List<BmCbpq> getCbPqInfo() {
        List<BmCbb> list=getCbbInfo(""); //得到所有的抄表本
        List<String> sscbpqList=list.stream().map(BmCbb::getSscbpq).collect(Collectors.toList());
       if(sscbpqList!=null && sscbpqList.size()>0){
           return bmCbpqService.list(Wrappers.<BmCbpq>lambdaQuery().in(BmCbpq::getCbpqbm,sscbpqList).orderByAsc(BmCbpq::getCbpqbm));
       }
       return new ArrayList<>();

    }

    @Override
    public List<BmCbb> getCbbInfo(String sscbpq) {
        Long userId= SecurityUtils.getUserId();
        List<String> stringList=getConfigUserId(userId);
      return bmCbbService.list(Wrappers.<BmCbb>lambdaQuery().in(BmCbb::getCbbbm,stringList)
                      .eq(StringUtil.isNotBlank(sscbpq),BmCbb::getSscbpq,sscbpq)
                .eq(BmCbb::getIsdisabled,"1").orderByAsc(BmCbb::getCbbbm));

    }
}
