/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.depotZoneFutures.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.depotZoneFutures.entity.DepotZoneFutures;
import vip.xiaonuo.biz.modular.depotZoneFutures.mapper.DepotZoneFuturesMapper;
import vip.xiaonuo.biz.modular.depotZoneFutures.param.DepotZoneFuturesAddParam;
import vip.xiaonuo.biz.modular.depotZoneFutures.param.DepotZoneFuturesEditParam;
import vip.xiaonuo.biz.modular.depotZoneFutures.param.DepotZoneFuturesIdParam;
import vip.xiaonuo.biz.modular.depotZoneFutures.param.DepotZoneFuturesPageParam;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 期货信息Service接口实现类
 *
 * @author xiang
 * @date 2024/01/10 09:54
 **/
@Service
public class DepotZoneFuturesService extends ServiceImpl<DepotZoneFuturesMapper, DepotZoneFutures> {

    public Page<DepotZoneFutures> page(DepotZoneFuturesPageParam depotZoneFuturesPageParam) {
        QueryWrapper<DepotZoneFutures> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(depotZoneFuturesPageParam.getFuturesCode())) {
            queryWrapper.lambda().like(DepotZoneFutures::getFuturesCode, depotZoneFuturesPageParam.getFuturesCode());
        }
        if (ObjectUtil.isNotEmpty(depotZoneFuturesPageParam.getFuturesName())) {
            queryWrapper.lambda().like(DepotZoneFutures::getFuturesName, depotZoneFuturesPageParam.getFuturesName());
        }
        if (ObjectUtil.isNotEmpty(depotZoneFuturesPageParam.getFuturesUnit())) {
            queryWrapper.lambda().like(DepotZoneFutures::getFuturesUnit, depotZoneFuturesPageParam.getFuturesUnit());
        }
        if (ObjectUtil.isNotEmpty(depotZoneFuturesPageParam.getRemark())) {
            queryWrapper.lambda().like(DepotZoneFutures::getRemark, depotZoneFuturesPageParam.getRemark());
        }
        if (ObjectUtil.isAllNotEmpty(depotZoneFuturesPageParam.getSortField(), depotZoneFuturesPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(depotZoneFuturesPageParam.getSortOrder());
            queryWrapper.orderBy(true, depotZoneFuturesPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(depotZoneFuturesPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(DepotZoneFutures::getUpdateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    public List<DepotZoneFutures> lst(DepotZoneFutures param) {
        return list(new LambdaQueryWrapper<>(param).orderByDesc(DepotZoneFutures::getUpdateTime));
    }

    public void add(DepotZoneFuturesAddParam depotZoneFuturesAddParam) {
        repeat(null, depotZoneFuturesAddParam.getFuturesCode());
        DepotZoneFutures depotZoneFutures = BeanUtil.toBean(depotZoneFuturesAddParam, DepotZoneFutures.class);
        depotZoneFutures.setFuturesPrice(new BigDecimal(depotZoneFuturesAddParam.getFuturesPrice()).setScale(2).toPlainString());
        depotZoneFutures.setCreateTime(new Date());
        depotZoneFutures.setUpdateTime(depotZoneFutures.getCreateTime());
        this.save(depotZoneFutures);
    }

    public void edit(DepotZoneFuturesEditParam depotZoneFuturesEditParam) {
        repeat(depotZoneFuturesEditParam.getId(), depotZoneFuturesEditParam.getFuturesCode());
        DepotZoneFutures depotZoneFutures = this.queryEntity(depotZoneFuturesEditParam.getId());
        BeanUtil.copyProperties(depotZoneFuturesEditParam, depotZoneFutures);
        depotZoneFutures.setFuturesPrice(new BigDecimal(depotZoneFuturesEditParam.getFuturesPrice()).setScale(2).toPlainString());
        this.updateById(depotZoneFutures);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(List<DepotZoneFuturesIdParam> depotZoneFuturesIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(depotZoneFuturesIdParamList, DepotZoneFuturesIdParam::getId));
    }

    public DepotZoneFutures detail(DepotZoneFuturesIdParam depotZoneFuturesIdParam) {
        return this.queryEntity(depotZoneFuturesIdParam.getId());
    }

    public DepotZoneFutures queryEntity(String id) {
        DepotZoneFutures depotZoneFutures = this.getById(id);
        if (ObjectUtil.isEmpty(depotZoneFutures)) {
            throw new CommonException("期货信息不存在，id值为：{}", id);
        }
        return depotZoneFutures;
    }

    /**
     * 重复记录检查
     *
     * @author xiang
     * @date 2023/12/06 09:02
     **/
    public void repeat(String id, String futuresCode) {
        LambdaQueryWrapper<DepotZoneFutures> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(Strings.isNotBlank(id), DepotZoneFutures::getId, id);
        wrapper.eq(Strings.isNotBlank(futuresCode), DepotZoneFutures::getFuturesCode, futuresCode);
        if (!list(wrapper).isEmpty())
            throw new CommonException("期货信息代码重复，代码：{}", futuresCode);
    }
}
