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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.sfgl.entity.Plsfkhxx;
import com.tofly.feesapi.sfgl.entity.vo.PlsfkhxxPageReq;
import com.tofly.feesapi.sfgl.entity.vo.PlsfkhxxReq;
import com.tofly.feesapi.sfgl.mapper.PlsfkhxxMapper;
import com.tofly.feesapi.sfgl.service.PlsfkhxxService;
import com.tofly.feesapi.yhgl.entity.vo.KhxxBdReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class PlsfkhxxServiceImpl extends ServiceImpl<PlsfkhxxMapper, Plsfkhxx> implements PlsfkhxxService {


    @Override
    public void add(PlsfkhxxReq plsfkhxxReq) {
        Plsfkhxx plsfkhxx = BeanUtil.copyProperties(plsfkhxxReq, Plsfkhxx.class);
        plsfkhxx.setId(IdUtil.simpleUUID());
        plsfkhxx.setSfkp("1");
//        限重
//        String fpBankcode = plsfkhxx.getFpBankcode();
//        String fpBankname = plsfkhxx.getFpBankname();
//        String fpNsrsbh = plsfkhxx.getFpNsrsbh();
//        LambdaQueryWrapper<Plsfkhxx> wrapper = Wrappers.<Plsfkhxx>lambdaQuery().eq(StrUtil.isNotEmpty(fpBankcode), Plsfkhxx::getFpBankcode, fpBankcode)
//                .eq(StrUtil.isNotEmpty(fpBankname), Plsfkhxx::getFpBankname, fpBankname)
//                .eq(StrUtil.isNotEmpty(fpNsrsbh), Plsfkhxx::getFpNsrsbh, fpNsrsbh);
//
//        Long count = baseMapper.selectCount(wrapper);

        save(plsfkhxx);
    }

    @Override
    public PageResult<Plsfkhxx> selectPage(PlsfkhxxPageReq plsfkhxxPageReq) {
        PageDto<Plsfkhxx> pageDto = plsfkhxxPageReq.getPageDto();

        String keyword = plsfkhxxPageReq.getKeyword();

        LambdaQueryWrapper<Plsfkhxx> wrapper = Wrappers.<Plsfkhxx>lambdaQuery()
                .like(StrUtil.isNotEmpty(keyword), Plsfkhxx::getGfxm, keyword)
                .or()
                .like(StrUtil.isNotEmpty(keyword), Plsfkhxx::getGfdh, keyword);

        PageDto<Plsfkhxx> pageResult = baseMapper.selectPage(pageDto, wrapper);
        if (ObjUtil.isNotNull(pageResult) && CollectionUtil.isNotEmpty(pageResult.getRecords())) {
            List<Plsfkhxx> records = pageResult.getRecords();
            records.forEach(o -> {
                List<String> list = StrUtil.split(o.getCbbbms(), StrUtil.COMMA);
                o.setCbbbmList(list);
            });
        }

        return pageResult.toVo();
    }

    @Override
    public void bind(KhxxBdReq bdReq) {
        String id = bdReq.getId();
        if (StrUtil.isEmpty(id)) {
            throw new FeesException("请选择要绑定的记录", FeesErrorCode.BUSINESS_ERROR);
        }

        List<String> cbbbms = bdReq.getCbbbms();
        if (CollectionUtil.isEmpty(cbbbms)) {
            throw new FeesException("请选择要绑定的抄表本", FeesErrorCode.BUSINESS_ERROR);
        }
        Plsfkhxx plsfkhxx = baseMapper.selectById(id);
        if (ObjUtil.isNull(plsfkhxx)) {
            throw new FeesException("没有查询到需要绑定的记录", FeesErrorCode.BUSINESS_ERROR);
        }

        List<Plsfkhxx> plsfkhxxList = this.lambdaQuery()
                .select(Plsfkhxx::getGfxm, Plsfkhxx::getCbbbms)
                .ne(Plsfkhxx::getId, id)
                .list();
        if (!plsfkhxxList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            plsfkhxxList.forEach(o -> {
                String cbbbmList = o.getCbbbms();
                if (StringUtil.isNotEmpty(cbbbmList)) {
                    List<String> list = StrUtil.split(cbbbmList, StrUtil.COMMA);
                    StringBuilder strcbbbms = new StringBuilder();
                    cbbbms.forEach(cbbbm -> {
                        if (list.contains(cbbbm)) {
                            strcbbbms.append(cbbbm).append("、");
                        }
                    });
                    if (strcbbbms.length() > 0) {
                        //去掉最后一个
                        strcbbbms.deleteCharAt(strcbbbms.length() - 1);
                        sb.append(String.format("%s已绑定抄表本%s", o.getGfxm(), strcbbbms)).append(";");
                    }
                }
            });
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
                throw new FeesException(sb.toString(), FeesErrorCode.BUSINESS_ERROR);
            }
        }
        String join = CollectionUtil.join(cbbbms, StrUtil.COMMA);
        plsfkhxx.setCbbbms(join);
        baseMapper.updateById(plsfkhxx);
    }

    @Override
    public Plsfkhxx getUserInfoByCode(String sscbb) {
        LambdaQueryWrapper<Plsfkhxx> wrapper = Wrappers.<Plsfkhxx>lambdaQuery()
                .apply(String.format("regexp_like(CBBBMS, '(^|,)'||'%s'||'(,|$)')", sscbb));
        return baseMapper.selectOne(wrapper);
    }

}
