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

import lombok.extern.slf4j.Slf4j;
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.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.payment.sys.domain.SysUnit;
import org.dromara.payment.sys.domain.bo.SysUnitBo;
import org.dromara.payment.sys.domain.vo.SysUnitVo;
import org.dromara.payment.sys.mapper.SysUnitMapper;
import org.dromara.payment.sys.service.ISysUnitService;
import org.dromara.settlement.api.RemoteConfigService;
import org.dromara.settlement.api.domain.Result;
import org.dromara.settlement.api.domain.ResultCodeEnum;
import org.dromara.settlement.api.domain.UnitConfDetail;
import org.dromara.settlement.api.domain.UnitConfGroup;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 单位基础信息Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-27
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class SysUnitServiceImpl implements ISysUnitService {

    private final SysUnitMapper baseMapper;

    private  RemoteConfigService configService;

    /**
     * 查询单位基础信息
     */
    @Override
    public SysUnitVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询单位基础信息列表
     */
    @Override
    public TableDataInfo<SysUnitVo> queryPageList(SysUnitBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUnit> lqw = buildQueryWrapper(bo);
        Page<SysUnitVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询单位基础信息列表
     */
    @Override
    public List<SysUnitVo> queryList(SysUnitBo bo) {
        LambdaQueryWrapper<SysUnit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUnit> buildQueryWrapper(SysUnitBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysUnit> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysUnit::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), SysUnit::getNo, bo.getNo());
        lqw.eq(bo.getUnitType() != null, SysUnit::getUnitType, bo.getUnitType());
        lqw.eq(StringUtils.isNotBlank(bo.getCustNo()), SysUnit::getCustNo, bo.getCustNo());
        return lqw;
    }

    /**
     * 新增单位基础信息
     */
    @Override
    public Boolean insertByBo(SysUnitBo bo) {
        SysUnit add = MapstructUtils.convert(bo, SysUnit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改单位基础信息
     */
    @Override
    public Boolean updateByBo(SysUnitBo bo) {
        SysUnit update = MapstructUtils.convert(bo, SysUnit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<UnitConfGroup> getConfGroupByUnit(Long unit_id, String class_alias) {
        try {
            Result<List<UnitConfGroup>> result = configService.getConfGroupByUnit(unit_id,class_alias);
            if(result.getRtnCode()== ResultCodeEnum.SUCCESS.Code()) {
                return result.getRtnData();
            }
        }catch(Exception ex) {
            String msgStr = "获取单位配置大类下面的组信息异常";
            log.error(msgStr,ex);
        }
        return null;
    }

    @Override
    public List<UnitConfDetail> getConfDetailByUnit(Long unit_id, String conf_group) {
        try {
            Result<List<UnitConfDetail>>  result = configService.getConfDetailByUnit(unit_id,conf_group);
            if(result.getRtnCode()==ResultCodeEnum.SUCCESS.Code()) {
                return result.getRtnData();
            }
        }catch(Exception ex) {
            String msgStr = "根据配置组查询单位配置项明细异常";
            log.error(msgStr,ex);
        }
        return null;
    }

    @Override
    public Boolean editConfDetail(List<UnitConfDetail> lendReco) {
        try {
            Result<Boolean>  result = configService.editConfDetail(lendReco);
            if(result.getRtnCode()==ResultCodeEnum.SUCCESS.Code()) {
                if(result.getRtnData()) {
                    return true;
                }
            }
        }catch(Exception ex) {
            String msgStr = "编辑单位配置项明细异常";
            log.error(msgStr,ex);
        }
        return false;
    }
}
