package top.continew.admin.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.PlantformBalanceMapper;
import top.continew.admin.business.model.entity.PlantformBalanceDO;
import top.continew.admin.business.model.query.PlantformBalanceQuery;
import top.continew.admin.business.model.req.CheckPlantBalanceReq;
import top.continew.admin.business.model.req.PlantformBalanceAddReq;
import top.continew.admin.business.model.req.PlantformBalanceEditReq;
import top.continew.admin.business.model.resp.PlantformBalanceResp;
import top.continew.admin.business.service.PlantformBalanceService;
import top.continew.starter.data.mp.util.QueryWrapperHelper;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.Collections;
import java.util.List;

/**
 * business业务实现
 *
 * @author jiay
 * @since 2025/06/24 16:47
 */
@Service
@RequiredArgsConstructor
public class PlantformBalanceServiceImpl extends ServiceImpl<PlantformBalanceMapper, PlantformBalanceDO> implements PlantformBalanceService {

    private final PlantformBalanceMapper plantformBalanceMapper;

    @Override
    public List<PlantformBalanceResp> getPlantformBalanceList(PlantformBalanceQuery query) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        List list = this.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, PlantformBalanceResp.class);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean addPlantformBalance(PlantformBalanceAddReq req) {
        //插入记录到数据库
        PlantformBalanceDO entity = BeanUtil.copyProperties(req, PlantformBalanceDO.class);
        return plantformBalanceMapper.insert(entity) > 0;

    }

    @Override
    public boolean editPlantformBalance(PlantformBalanceEditReq req) {
        PlantformBalanceDO PlantformBalanceDO = BeanUtil.copyProperties(req, PlantformBalanceDO.class);
        return plantformBalanceMapper.updateById(PlantformBalanceDO) > 0;

    }

    @Override
    public boolean deletePlantformBalance(Long id) {
        if (id != null) {
            return plantformBalanceMapper.deleteById(id) > 0;
        }
        return false;
    }

    @Override
    public PlantformBalanceResp getPlantformBalance(Long id) {
        PlantformBalanceDO PlantformBalanceDO = plantformBalanceMapper.selectById(id);
        PlantformBalanceResp PlantformBalanceResp = new PlantformBalanceResp();
        if (PlantformBalanceDO != null) {
            PlantformBalanceResp = BeanUtil.copyProperties(PlantformBalanceDO, PlantformBalanceResp.class);
        }
        return PlantformBalanceResp;
    }

    @Override
    public PageResp<PlantformBalanceResp> getPlantformBalancePage(PlantformBalanceQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        this.sort(queryWrapper, pageQuery.getSort());
        Page page = plantformBalanceMapper.selectPage(new Page<>(pageQuery.getPage(), pageQuery
            .getSize()), queryWrapper);
        return PageResp.build(page);
    }

    /**
     * 设置排序
     *
     * @param queryWrapper 查询条件封装对象
     * @param sort         排序查询条件
     */
    private void sort(QueryWrapper queryWrapper, Sort sort) {
        if (sort == null || sort.isUnsorted()) {
            return;
        }
        for (Sort.Order order : sort) {
            String property = order.getProperty();
            queryWrapper.orderBy(true, order.isAscending(), CharSequenceUtil.toUnderlineCase(property));
        }
    }

    @Override
    public List<PlantformBalanceResp> getUserBalancesByUserId(Long userId) {
        LambdaQueryWrapper<PlantformBalanceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlantformBalanceDO::getUserId, userId);
        List<PlantformBalanceDO> list = this.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, PlantformBalanceResp.class);
        }
        return Collections.emptyList();
    }

    @Override
    public R checkPlantBalance(CheckPlantBalanceReq req) {
        QueryWrapper<PlantformBalanceDO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", req.getUserId());
        queryWrapper.eq("charge_type", req.getChargeType());
        PlantformBalanceDO one = plantformBalanceMapper.selectOne(queryWrapper);
        if (one == null) {
            PlantformBalanceAddReq addReq = new PlantformBalanceAddReq().initPlantformBalance(req);
            this.addPlantformBalance(addReq);
        }
        if (one.getBalance() < req.getChargePoints()) {
            return R.fail(R.fail().getCode(), "余额不足，请充值！");
        }
        return R.ok();
    }

}