/*
 * 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.depotZonePrice.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.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.core.exception.BizExceptionEnum;
import vip.xiaonuo.biz.modular.depotGrainInfoPrice.service.DepotGrainInfoPriceService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.depotZonePrice.entity.DepotZonePrice;
import vip.xiaonuo.biz.modular.depotZonePrice.mapper.DepotZonePriceMapper;
import vip.xiaonuo.biz.modular.depotZonePrice.param.DepotZonePriceAddParam;
import vip.xiaonuo.biz.modular.depotZonePrice.param.DepotZonePriceEditParam;
import vip.xiaonuo.biz.modular.depotZonePrice.param.DepotZonePriceIdParam;
import vip.xiaonuo.biz.modular.depotZonePrice.param.DepotZonePricePageParam;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 区域价格Service接口实现类
 *
 * @author xiang
 * @date 2023/09/18 17:38
 **/
@Service
public class DepotZonePriceService extends ServiceImpl<DepotZonePriceMapper, DepotZonePrice> {

    @Resource
    DepotGrainInfoPriceService grainInfoPriceService;

    public Page<DepotZonePrice> page(DepotZonePricePageParam depotZonePricePageParam) {
        QueryWrapper<DepotZonePrice> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(depotZonePricePageParam.getZoneName())) {
            queryWrapper.lambda().like(DepotZonePrice::getZoneName, depotZonePricePageParam.getZoneName());
        }
        if (ObjectUtil.isNotEmpty(depotZonePricePageParam.getGrainGroup())) {
            queryWrapper.lambda().eq(DepotZonePrice::getGrainGroup, depotZonePricePageParam.getGrainGroup());
        }
        if (ObjectUtil.isAllNotEmpty(depotZonePricePageParam.getSortField(), depotZonePricePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(depotZonePricePageParam.getSortOrder());
            queryWrapper.orderBy(true, depotZonePricePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(depotZonePricePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(DepotZonePrice::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    public List<DepotZonePrice> lst(DepotZonePrice param) {
        return list(new LambdaQueryWrapper<>(param));
    }

    public List<DepotZonePrice> lst(List<String> idLst) {
        return listByIds(idLst);
    }

    public BigDecimal getPriceById(String id) {
        DepotZonePrice price = getById(id);
        return price == null ? BigDecimal.ZERO : price.getZonePrice();
    }

    public void add(DepotZonePriceAddParam param) {
        checkNameRepeat(null, param.getZoneName(), param.getGrainGroup(), true);
        DepotZonePrice depotZonePrice = BeanUtil.toBean(param, DepotZonePrice.class);
        this.save(depotZonePrice);
    }

    public void edit(DepotZonePriceEditParam param) {
        checkNameRepeat(param.getId(), param.getZoneName(), param.getGrainGroup(), true);
        DepotZonePrice depotZonePrice = this.queryEntity(param.getId());
        if (param.getZonePrice().compareTo(depotZonePrice.getZonePrice()) != 0)
            grainInfoPriceService.editBatchByZonePriceId(param.getId(), param.getZonePrice());
        BeanUtil.copyProperties(param, depotZonePrice);
        this.updateById(depotZonePrice);
    }

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

    public DepotZonePrice detail(DepotZonePriceIdParam depotZonePriceIdParam) {
        return this.queryEntity(depotZonePriceIdParam.getId());
    }

    public DepotZonePrice queryEntity(String id) {
        DepotZonePrice depotZonePrice = this.getById(id);
        if (ObjectUtil.isEmpty(depotZonePrice)) {
            throw new CommonException("区域价格不存在，id值为：{}", id);
        }
        return depotZonePrice;
    }

    /**
     * 检查名称重复
     *
     * @param throwFlag true:抛异常
     * @return true:重复
     * @author xiang
     * @date 2023-01-01
     */
    private boolean checkNameRepeat(String id, String name, String grainGroup, boolean throwFlag) {
        List<DepotZonePrice> list = lst(new DepotZonePrice(name, grainGroup));
        if (list.isEmpty()) return false;
        if (Objects.equals(list.get(0).getId(), id) && list.size() == 1) return false;
        if (!throwFlag) return true;
        throw new CommonException(BizExceptionEnum.ERROR00009.getMessage());
    }

}
