package org.dromara.payment.invoice.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.payment.operator.domain.bo.ReProductInvoiceBo;
import org.springframework.stereotype.Service;
import org.dromara.payment.invoice.domain.bo.MerInvoiceTypeBo;
import org.dromara.payment.invoice.domain.vo.MerInvoiceTypeVo;
import org.dromara.payment.invoice.domain.MerInvoiceType;
import org.dromara.payment.invoice.mapper.MerInvoiceTypeMapper;
import org.dromara.payment.invoice.service.IMerInvoiceTypeService;

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

/**
 * 发票类目Service业务层处理
 *
 * @author LionLi
 * @date 2024-05-06
 */
@RequiredArgsConstructor
@Service
public class MerInvoiceTypeServiceImpl implements IMerInvoiceTypeService {

    private final MerInvoiceTypeMapper baseMapper;

    /**
     * 查询发票类目
     */
    @Override
    public MerInvoiceTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发票类目列表
     */
    @Override
    public TableDataInfo<MerInvoiceTypeVo> queryPageList(MerInvoiceTypeBo bo, PageQuery pageQuery) {
        QueryWrapper<MerInvoiceType> lqw = buildQueryWrapper(bo);
        Page<MerInvoiceTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发票类目列表
     */
    @Override
    public List<MerInvoiceTypeVo> queryList(MerInvoiceTypeBo bo) {
        QueryWrapper<MerInvoiceType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<MerInvoiceType> buildQueryWrapper(MerInvoiceTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MerInvoiceType> lqw = new QueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getParentType()), "parent_type", bo.getParentType());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), "type", bo.getType());
        lqw.eq(bo.getSort() != null, "sort", bo.getSort());
        return lqw;
    }

    /**
     * 新增发票类目
     */
    @Override
    public Boolean insertByBo(MerInvoiceTypeBo bo) {
        MerInvoiceType add = MapstructUtils.convert(bo, MerInvoiceType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发票类目
     */
    @Override
    public Boolean updateByBo(MerInvoiceTypeBo bo) {
        MerInvoiceType update = MapstructUtils.convert(bo, MerInvoiceType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MerInvoiceType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除发票类目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    public List<Map> kp(Integer proId){
        List<MerInvoiceTypeVo> list =  this.baseMapper.selectInvoiceListByProId(proId);

        List<Map> resultMapList  =  new ArrayList<>();

        list.stream().forEach(item->{
            boolean tag = true;
            for(Map mp :resultMapList){
                if(item.getParentType().equals(mp.get("value"))){
                    ((List)mp.get("children")).add(new HashMap<>(){
                        {
                            put("value",item.getId());
                            put("label",item.getType());
                        }
                    });
                    tag = false;
                    break;
                }
            }

            if(tag){
                resultMapList.add(new HashMap(){{
                    put("value",item.getParentType());
                    put("label",item.getParentType());
                    put("children",new ArrayList<>());
                }});

            }
        });
        return resultMapList;
    }


    @Override
    public List<Map> getListInvoice(MerInvoiceTypeBo bo) {

         List<MerInvoiceTypeVo> list = this.baseMapper.selectVoList();

         List<Map> resultMapList  =  new ArrayList<>();

        list.stream().forEach(item->{
            boolean tag = true;
            for(Map mp :resultMapList){
                if(item.getParentType().equals(mp.get("value"))){
                    ((List)mp.get("children")).add(new HashMap<>(){
                        {
                            put("value",item.getId());
                            put("label",item.getType());
                        }
                    });
                    tag = false;
                    break;
                }
            }

            if(tag){
                resultMapList.add(new HashMap(){{
                    put("value",item.getParentType());
                    put("label",item.getParentType());
                    put("children",new ArrayList<>());
                }});

            }
        });
        return resultMapList;
    }




    @Override
    public List<MerInvoiceTypeVo> selectList(int busType, String busId,int proId) {
        return baseMapper.selectInvoiceByProId(busType,busId,proId,busId.split(",").length);
    }
}
