package cn.ysk.cashier.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.mapper.SysParamsMapper;
import cn.ysk.cashier.system.domain.Params;
import cn.ysk.cashier.system.service.ParamsService;
import cn.ysk.cashier.system.service.dto.ParamsDTO;
import cn.ysk.cashier.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 参数管理
 *
 * @author tankaikai
 * @since 2024-09-27 11:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ParamsServiceImpl extends ServiceImpl<SysParamsMapper, Params> implements ParamsService {

    private QueryWrapper<Params> getWrapper(Map<String, Object> params) {

        MapProxy mapProxy = MapProxy.create(params);
        QueryWrapper<Params> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Params::getParamType, 1);

        String paramCode = mapProxy.getStr("paramCode");
        wrapper.lambda().like(StrUtil.isNotEmpty(paramCode), Params::getParamCode, paramCode);

        wrapper.lambda().orderByDesc(Params::getParamId);

        return wrapper;
    }


    @Override
    public Map<String, Object> page(Map<String, Object> params) {
        MapProxy mapProxy = MapProxy.create(params);
        int pageNum = mapProxy.getInt("page", 1);
        int pageSize = mapProxy.getInt("size", 10);
        Page<Params> page = super.page(new Page<>(pageNum, pageSize), getWrapper(params));
        List<ParamsDTO> list = BeanUtil.copyToList(page.getRecords(), ParamsDTO.class);
        return PageUtil.toPlusPage(list, Convert.toInt(page.getTotal()));
    }

    @Override
    public List<ParamsDTO> list(Map<String, Object> params) {
        List<Params> list = super.list(getWrapper(params));
        return BeanUtil.copyToList(list, ParamsDTO.class);
    }

    @Override
    public String getValue(String paramCode) {
        Params params = baseMapper.selectOne(Wrappers.<Params>lambdaQuery().select(Params::getParamValue).eq(Params::getParamCode, paramCode));
        return Optional.ofNullable(params).map(Params::getParamValue).orElseThrow(() -> new BadRequestException("找不到对应的系统参数值"));
    }

    @Override
    public <T> T getValueObject(String paramCode, Class<T> clazz) {
        String paramValue = getValue(paramCode);
        if (StringUtils.isNotBlank(paramValue)) {
            return JSONUtil.toBean(paramValue, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new BadRequestException("获取参数失败");
        }
    }

    @Override
    public int updateValueByCode(String paramCode, String paramValue) {
        return baseMapper.update(null, Wrappers.<Params>lambdaUpdate().eq(Params::getParamCode, paramCode).set(Params::getParamValue, paramValue));
    }
}
