/*
 * 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.bizsaleorder.service.impl;

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.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.bizsaleorder.entity.BizSaleOrder;
import vip.xiaonuo.biz.modular.bizsaleorder.mapper.BizSaleOrderMapper;
import vip.xiaonuo.biz.modular.bizsaleorder.param.BizSaleOrderAddParam;
import vip.xiaonuo.biz.modular.bizsaleorder.param.BizSaleOrderEditParam;
import vip.xiaonuo.biz.modular.bizsaleorder.param.BizSaleOrderIdParam;
import vip.xiaonuo.biz.modular.bizsaleorder.param.BizSaleOrderPageParam;
import vip.xiaonuo.biz.modular.bizsaleorder.service.BizSaleOrderService;

import javax.annotation.RegEx;
import java.math.BigDecimal;
import java.util.List;

/**
 * 销售订单Service接口实现类
 *
 * @author fanzherong
 * @date  2025/04/10 09:31
 **/
@Service
public class BizSaleOrderServiceImpl extends ServiceImpl<BizSaleOrderMapper, BizSaleOrder> implements BizSaleOrderService {

    @Resource
    private BizGoodsService goodsService;

    @Override
    public Page<BizSaleOrder> page(BizSaleOrderPageParam bizSaleOrderPageParam) {
        QueryWrapper<BizSaleOrder> queryWrapper = new QueryWrapper<BizSaleOrder>().checkSqlInjection();
        //订单编号查询
        if(ObjectUtil.isNotEmpty(bizSaleOrderPageParam.getSaleOrderNumber())){
            queryWrapper.like("bso.sale_order_number",bizSaleOrderPageParam.getSaleOrderNumber());
        }
        //订单名称查询
        if(ObjectUtil.isNotEmpty(bizSaleOrderPageParam.getSaleOrderName())){
            queryWrapper.like("bso.sale_order_name",bizSaleOrderPageParam.getSaleOrderName());
        }
        //订单类型查询
        if(ObjectUtil.isNotEmpty(bizSaleOrderPageParam.getSaleOrderType())){
            queryWrapper.eq("bso.sale_order_type",bizSaleOrderPageParam.getSaleOrderType());
        }
        //货品名称查询
        if(ObjectUtil.isNotEmpty(bizSaleOrderPageParam.getSaleGoodsName())){
            queryWrapper.like("bso.sale_goods_name",bizSaleOrderPageParam.getSaleGoodsName());
        }
        queryWrapper.eq("bso.delete_flag","NOT_DELETE");
        queryWrapper.orderByDesc("bso.create_time");
        return this.getBaseMapper().getPage(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizSaleOrderAddParam bizSaleOrderAddParam) {
        long count = this.count(new QueryWrapper<BizSaleOrder>().lambda().
                eq(BizSaleOrder::getSaleOrderNumber, bizSaleOrderAddParam.getSaleOrderNumber()));
        if(count>0){
            throw new CommonException("订单编号已经存在！");
        }
        BizSaleOrder bizSaleOrder = BeanUtil.toBean(bizSaleOrderAddParam, BizSaleOrder.class);
        BizGoods bizGoods = goodsService.getOne(new QueryWrapper<BizGoods>().lambda()
                .eq(BizGoods::getGoodsCode, bizSaleOrderAddParam.getSaleGoodsCode()).
                last("limit 1"));
        if(ObjectUtil.isNotNull(bizGoods)){
            bizSaleOrder.setSaleGoodsName(bizGoods.getGoodsName());
        }
        bizSaleOrder.setSaleOrderWeight(bizSaleOrderAddParam.getSaleOrderWeight().multiply(new BigDecimal(1000)));
        this.save(bizSaleOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizSaleOrderEditParam bizSaleOrderEditParam) {
        BizSaleOrder bizSaleOrder = this.queryEntity(bizSaleOrderEditParam.getId());
        if(!StringUtils.equals(bizSaleOrder.getSaleOrderNumber(),bizSaleOrderEditParam.getSaleOrderNumber())){
            long count = this.count(new QueryWrapper<BizSaleOrder>().lambda().
                    eq(BizSaleOrder::getSaleOrderNumber, bizSaleOrderEditParam.getSaleOrderNumber()));
            if(count>0){
                throw new CommonException("订单编号已经存在！");
            }
        }
        BeanUtil.copyProperties(bizSaleOrderEditParam, bizSaleOrder);
        bizSaleOrder.setSaleOrderWeight(bizSaleOrderEditParam.getSaleOrderWeight().multiply(new BigDecimal(1000)));
        BizGoods bizGoods = goodsService.getOne(new QueryWrapper<BizGoods>().lambda()
                .eq(BizGoods::getGoodsCode, bizSaleOrderEditParam.getSaleGoodsCode()).
                last("limit 1"));
        if(ObjectUtil.isNotNull(bizGoods)){
            bizSaleOrder.setSaleGoodsName(bizGoods.getGoodsName());
        }
        this.updateById(bizSaleOrder);
    }

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

    @Override
    public BizSaleOrder detail(BizSaleOrderIdParam bizSaleOrderIdParam) {
        BizSaleOrder bizSaleOrder = this.queryEntity(bizSaleOrderIdParam.getId());
        return bizSaleOrder;
    }

    @Override
    public BizSaleOrder queryEntity(String id) {
        BizSaleOrder bizSaleOrder = this.getById(id);
        if(ObjectUtil.isEmpty(bizSaleOrder)) {
            throw new CommonException("销售订单不存在，id值为：{}", id);
        }
        return bizSaleOrder;
    }

    @Override
    public List<BizSaleOrder> queryByCustomerId(BizSaleOrderIdParam bizSaleOrderIdParam) {
        QueryWrapper<BizSaleOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BizSaleOrder::getCustomerId, bizSaleOrderIdParam.getId());
        if(StringUtils.equals(bizSaleOrderIdParam.getFlag(),"add")){
            queryWrapper.lambda().eq(BizSaleOrder::getSaleStatus, "1");
        }
        //根据客户id查询待执行状态的销售订单
        List<BizSaleOrder> list = this.list(queryWrapper);
        for(BizSaleOrder bizSaleOrder : list){
            bizSaleOrder.setSaleOrderWeight(bizSaleOrder.getSaleOrderWeight().divide(new BigDecimal(1000)));
        }
        return list;
    }

    @Override
    public BizSaleOrder detailById(BizSaleOrderIdParam bizSaleOrderIdParam) {
        BizSaleOrder bizSaleOrder = this.queryEntity(bizSaleOrderIdParam.getId());
        bizSaleOrder.setSaleOrderWeight(bizSaleOrder.getSaleOrderWeight().divide(new BigDecimal(1000)));
        return bizSaleOrder;
    }
}
