package org.jeecg.modules.xcx.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hotelcategory.entity.HotelCategory;
import org.jeecg.modules.hotelcategory.service.IHotelCategoryService;
import org.jeecg.modules.hotelconf.service.IHotelConfService;
import org.jeecg.modules.hotelgoods.entity.HotelGoods;
import org.jeecg.modules.hotelgoods.service.IHotelGoodsService;
import org.jeecg.modules.hotelgoodssku.entity.HotelGoodsSku;
import org.jeecg.modules.hotelgoodssku.service.IHotelGoodsSkuService;
import org.jeecg.modules.hotelorder.entity.HotelOrder;
import org.jeecg.modules.hotelshoppingcat.entity.HotelShoppingCat;
import org.jeecg.modules.hotelshoppingcat.mapper.HotelShoppingCatMapper;
import org.jeecg.modules.hotelshoppingcat.resp.ShoppingCartPageResp;
import org.jeecg.modules.hotelshoppingcat.service.IHotelShoppingCatService;
import org.jeecg.modules.xcx.goods.req.*;
import org.jeecg.modules.xcx.goods.service.IGoodsService;
import org.jeecgframework.codegenerate.a.a;
import org.springframework.stereotype.Service;

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

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Resource
    private IHotelGoodsService hotelGoodsService;
    @Resource
    private IHotelGoodsSkuService hotelGoodsSkuService;
    @Resource
    private IHotelShoppingCatService hotelShoppingCatService;
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private HotelShoppingCatMapper hotelShoppingCatMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private IHotelConfService hotelConfService;
    @Resource
    private IHotelCategoryService hotelCategoryService;



    @Override
    public Result<?> getGoodsList(GoodsListReq goodsListReq) {

        Page<HotelGoods> page = new Page<>(goodsListReq.getPageNo(), goodsListReq.getPageSize());
        LambdaQueryChainWrapper<HotelGoods> query = hotelGoodsService.lambdaQuery();
        query.eq(HotelGoods::getDelFlag, 0)
                .eq(HotelGoods::getUpFlag, 1);

        if(StringUtils.isNotBlank(goodsListReq.getShopId())){
            query.eq(HotelGoods::getShopId, goodsListReq.getShopId());
        }
        if(StringUtils.isNotBlank(goodsListReq.getName())){
            query.like(HotelGoods::getName,goodsListReq.getName());
        }

        Page<HotelGoods> hotelGoodsPage = query.orderByAsc(HotelGoods::getSort).orderByDesc(HotelGoods::getCreateTime).page(page);

        BigDecimal depositPrice = new BigDecimal(hotelConfService.getConf("depositPrice"));
        for (HotelGoods hotelGoods:hotelGoodsPage.getRecords()) {
            hotelGoods.setDepositPrice(depositPrice);
            hotelGoods.setGoodsSku(hotelGoodsSkuService.getHotelGoodsSkuOne(hotelGoods.getId()));
        }

        return Result.OK(hotelGoodsPage);
    }

    @Override
    public Result<?> getGoodsOne(GoodsDetailReq goodsDetailReq) {
        LambdaQueryChainWrapper<HotelGoods> queryChainWrapper = hotelGoodsService.lambdaQuery();
        queryChainWrapper.eq(HotelGoods::getDelFlag, 0);
        if(StringUtils.isNotBlank(goodsDetailReq.getId())){
            queryChainWrapper.eq(HotelGoods::getId, goodsDetailReq.getId());
        }
        if(StringUtils.isNotBlank(goodsDetailReq.getSkuId())){
            queryChainWrapper.eq(HotelGoods::getSkuId, goodsDetailReq.getSkuId());
        }
        HotelGoods hotelGoods = queryChainWrapper.one();
        List<HotelGoodsSku> hotelGoodsSkus = hotelGoodsSkuService.lambdaQuery()
                .eq(HotelGoodsSku::getDelFlag, 0)
                .eq(HotelGoodsSku::getGoodsId, hotelGoods.getId())
                .orderByAsc(HotelGoodsSku::getSort)
                .list();

        BigDecimal depositPrice = new BigDecimal(hotelConfService.getConf("depositPrice"));
        hotelGoods.setDepositPrice(depositPrice);

        HotelCategory hotelCategory = hotelCategoryService.lambdaQuery()
                .eq(HotelCategory::getDelFlag, 0)
                .eq(HotelCategory::getId, hotelGoods.getCategoryId())
                .one();
        hotelGoods.setCategoryName(hotelCategory.getName());

        hotelGoods.setHotelGoodsSkuList(hotelGoodsSkus);

        return Result.OK(hotelGoods);
    }

    @Override
    public Result<?> joinShoppingCart(JoinShoppingCartReq joinShoppingCartReq) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(joinShoppingCartReq.getToken());
        if(joinShoppingCartReq.getNum() == null || joinShoppingCartReq.getNum() == 0){
            joinShoppingCartReq.setNum(1);
        }
        HotelGoods hotelGoods = hotelGoodsService.lambdaQuery()
                .eq(HotelGoods::getDelFlag, 0)
                .eq(HotelGoods::getUpFlag, 1)
                .eq(HotelGoods::getId, joinShoppingCartReq.getId())
                .one();

        if(hotelGoods == null){
            throw new JeecgBootException("商品信息错误，请刷新重试");
        }

        HotelGoodsSku hotelGoodsSku = hotelGoodsSkuService.lambdaQuery()
                .eq(HotelGoodsSku::getDelFlag, 0)
                .eq(HotelGoodsSku::getGoodsId, hotelGoods.getId())
                .eq(HotelGoodsSku::getId, joinShoppingCartReq.getSkuId())
                .one();

        if(hotelGoodsSku == null){
            throw new JeecgBootException("规格错误，请刷新重试");
        }
        HotelShoppingCat hotelShoppingCat1 = hotelShoppingCatService.lambdaQuery()
                .eq(HotelShoppingCat::getDelFlag, 0)
                .eq(HotelShoppingCat::getGoodsId, hotelGoods.getId())
                .eq(HotelShoppingCat::getSkuId, hotelGoodsSku.getId())
                .eq(HotelShoppingCat::getUserId, hanHaiMember.getId())
                .one();
        HotelShoppingCat hotelShoppingCat = new HotelShoppingCat();
        if(hotelShoppingCat1 == null){

            hotelShoppingCat.setGoodsId(hotelGoods.getId());
            hotelShoppingCat.setSkuId(hotelGoodsSku.getId());
            hotelShoppingCat.setUserId(hanHaiMember.getId());
            hotelShoppingCat.setNum(joinShoppingCartReq.getNum());
            hotelShoppingCatService.save(hotelShoppingCat);
        }else{
            hotelShoppingCat.setId(hotelShoppingCat1.getId());
            hotelShoppingCat.setNum(hotelShoppingCat1.getNum() + joinShoppingCartReq.getNum());
            hotelShoppingCatService.updateById(hotelShoppingCat);
        }



        return Result.OK();
    }

    @Override
    public Result<?> shoppingCartPage(ShoppingCartPageReq shoppingCartPageReq) {

        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(shoppingCartPageReq.getToken());

        Page<ShoppingCartPageResp> page = new Page<ShoppingCartPageResp>(shoppingCartPageReq.getPageNo(), shoppingCartPageReq.getPageSize());

        IPage<ShoppingCartPageResp> shoppingCartPage = hotelShoppingCatMapper.getShoppingCartPage(page, hanHaiMember.getId());

        return Result.OK(shoppingCartPage);
    }

    @Override
    public Result<?> deleteShoppingCart(DeleteShoppingCartReq deleteShoppingCartReq) {

        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(deleteShoppingCartReq.getToken());
//        HotelShoppingCat hotelShoppingCat = hotelShoppingCatService.lambdaQuery()
//                .eq(HotelShoppingCat::getId, deleteShoppingCartReq.getId())
//                .eq(HotelShoppingCat::getUserId, hanHaiMember.getId())
//                .one();
//        if(hotelShoppingCat == null){
//            throw new JeecgBootException("购物车数据错误,请刷新重试");
//        }

        hotelShoppingCatService.removeBatchByIds(Arrays.asList(deleteShoppingCartReq.getId().split(",")));

        return Result.OK();
    }


    @Override
    public Result<?> addOrDeleteNum(AddOrDeleteNumReq addOrDeleteNumReq) {
        Integer n = (Integer) redisUtil.get("addOrDeleteNumReq:"+addOrDeleteNumReq.getToken());
        if(n != null){
            throw new JeecgBootException("请求频率太快");
        }
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(addOrDeleteNumReq.getToken());
        HotelShoppingCat hotelShoppingCat = hotelShoppingCatService.lambdaQuery()
                .eq(HotelShoppingCat::getDelFlag, 0)
                .eq(HotelShoppingCat::getUserId, hanHaiMember.getId())
                .eq(HotelShoppingCat::getId, addOrDeleteNumReq.getId())
                .one();
        if(hotelShoppingCat == null){
            throw new JeecgBootException("购物车数据错误，请刷新重试");
        }
        HotelShoppingCat hotelShoppingCat1 = new HotelShoppingCat();
        hotelShoppingCat1.setId(hotelShoppingCat.getId());
        if(addOrDeleteNumReq.getNum() != null){
            hotelShoppingCat1.setNum(addOrDeleteNumReq.getNum());
            if(addOrDeleteNumReq.getNum() == 0){
                hotelShoppingCat1.setDelFlag(1);
            }
        }

//        if (addOrDeleteNumReq.getType() != null) {
//            if(addOrDeleteNumReq.getType() == 0){
//                hotelShoppingCat1.setNum(hotelShoppingCat.getNum() + 1);
//            }else{
//                hotelShoppingCat1.setNum(hotelShoppingCat.getNum() - 1);
//                if(hotelShoppingCat.getNum() == 1){
//                    hotelShoppingCat1.setDelFlag(1);
//                }
//            }
//
//        }

        hotelShoppingCatService.updateById(hotelShoppingCat1);
        redisUtil.set("addOrDeleteNumReq:"+addOrDeleteNumReq.getToken(),1,1);
        return Result.OK();
    }

    /**
     * 获取商品多规格
     * @param id
     * @return
     */
    @Override
    public Result<?> getGoodsSku(String id) {
        List<HotelGoodsSku> hotelGoodsSkuList = hotelGoodsSkuService.lambdaQuery()
                .eq(HotelGoodsSku::getGoodsId, id)
                .eq(HotelGoodsSku::getDelFlag, 0)
                .list();
        return Result.OK(hotelGoodsSkuList);
    }

    @Override
    public Result<?> editShoppingCatByGoodsId(EditShoppingCatByGoodsIdReq editShoppingCatByGoodsIdReq) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(editShoppingCatByGoodsIdReq.getToken());
        HotelShoppingCat hotelShoppingCat = hotelShoppingCatService.lambdaQuery()
                .eq(HotelShoppingCat::getDelFlag, 0)
                .eq(HotelShoppingCat::getGoodsId, editShoppingCatByGoodsIdReq.getGoodsId())
                .eq(HotelShoppingCat::getUserId, hanHaiMember.getId())
                .eq(HotelShoppingCat::getId, editShoppingCatByGoodsIdReq.getId())
                .one();
        if(hotelShoppingCat == null){
            throw new JeecgBootException("数据错误，请稍后重试");
        }
        HotelShoppingCat hotelShoppingCat1 = new HotelShoppingCat();
        hotelShoppingCat1.setId(hotelShoppingCat.getId());
        hotelShoppingCat1.setSkuId(editShoppingCatByGoodsIdReq.getSkuId());
        hotelShoppingCatService.updateById(hotelShoppingCat1);
        return Result.OK();
    }
}
