package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.gxwebsoft.shop.entity.Goods;
import com.gxwebsoft.shop.entity.Merchant;
import com.gxwebsoft.shop.mapper.GoodsStockInMerchantMapper;
import com.gxwebsoft.shop.service.GoodsService;
import com.gxwebsoft.shop.service.GoodsStockInMerchantService;
import com.gxwebsoft.shop.entity.GoodsStockInMerchant;
import com.gxwebsoft.shop.param.GoodsStockInMerchantParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户商品库存Service实现
 *
 * @author 科技小王子
 * @since 2024-10-05 02:33:23
 */
@Service
public class GoodsStockInMerchantServiceImpl extends ServiceImpl<GoodsStockInMerchantMapper, GoodsStockInMerchant> implements GoodsStockInMerchantService {

    @Resource
    private GoodsService goodsService;
    @Override
    public PageResult<GoodsStockInMerchant> pageRel(GoodsStockInMerchantParam param) {
        PageParam<GoodsStockInMerchant, GoodsStockInMerchantParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<GoodsStockInMerchant> list = baseMapper.selectPageRel(page, param);
        for (GoodsStockInMerchant goodsStockInMerchant : list) {
            Goods goods = goodsService.getById(goodsStockInMerchant.getGoodsId());
            goodsStockInMerchant.setGoods(goods);
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<GoodsStockInMerchant> listRel(GoodsStockInMerchantParam param) {
        List<GoodsStockInMerchant> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<GoodsStockInMerchant, GoodsStockInMerchantParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public GoodsStockInMerchant getByIdRel(Integer id) {
        GoodsStockInMerchantParam param = new GoodsStockInMerchantParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public GoodsStockInMerchant check(Integer merchantId, Integer goodsId, Integer skuId) {
        GoodsStockInMerchantParam param = new GoodsStockInMerchantParam();
        param.setMerchantId(merchantId);
        param.setGoodsId(goodsId);
        param.setSkuId(skuId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public List<GoodsStockInMerchant> canExpressList(Integer goodsId, Integer stock, String lat, String lng) {
        String distanceSql = "ACOS(SIN(( " + lat + " * 3.1415) / 180 ) * SIN((lat * 3.1415) / 180 ) + " +
                "COS(( " + lat + " * 3.1415) / 180) * COS((b.lat * 3.1415) / 180 ) * COS(( " + lng + " * 3.1415) / 180 - " +
                "(b.lng * 3.1415) / 180 ) ) * 6380" +
                " as distance";
        return list(
                new MPJQueryWrapper<GoodsStockInMerchant>()
                        .leftJoin("shop_merchant AS b ON b.merchant_id = t.merchant_id")
                        .select("t.*", distanceSql)
                        .eq("goods_id", goodsId)
                        .eq("can_express", 1)
                        .ge("stock", stock)
                        .orderByDesc("distance")
        );
    }

}
