package org.jeecg.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.api.bean.PageBean;
import org.jeecg.api.service.AppletCatService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.commonAddress.entity.CommonAddress;
import org.jeecg.modules.commonCat.entity.CommonCat;
import org.jeecg.modules.commonCat.service.ICommonCatService;
import org.jeecg.modules.commonOrder.entity.CommonOrder;
import org.jeecg.modules.commonShop.entity.CommonShop;
import org.jeecg.modules.commonShop.service.ICommonShopService;
import org.jeecg.modules.commonShop.service.impl.CommonShopServiceImpl;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AppletCatServiceImpl implements AppletCatService {
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    @Resource
    private ICommonShopService commonShopService;
    @Resource
    private ICommonCatService commonCatService;


    //获取购物车信息列表带分页
    @Override
    public Result<?> getCartPageList(String token, PageBean pageBean){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        Page<CommonCat> page = commonCatService
                .lambdaQuery()
                .eq(CommonCat::getUserId,hanHaiMember.getId())
                .page(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()));
        //创建一个新的list集合
        List<CommonCat> records = new ArrayList<>();
        //循环得到信息
        for (CommonCat commonCart : page.getRecords()) {
            commonCart.setShop(commonShopService.getById(commonCart.getShopId()));
            records.add(commonCart);
        }
        page.setRecords(records);

        return Result.OK(page);
    }


    //加入购物车
    @Override
    public Result<?> addCart(String token, String shopId, Integer num){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //根据商品标识查询商品信息
        CommonShop commonShop = commonShopService.getById(shopId);
        if (commonShop==null){
            return Result.error("商品不存在");
        }
        //根据商品标识查询购物车是否存在该商品
        CommonCat commonCart = commonCatService.lambdaQuery()
                .eq(CommonCat::getUserId,hanHaiMember.getId())
                .eq(CommonCat::getShopId,shopId)
                .one();
        //如果有则修改数量
        if (commonCart!=null){
            commonCart.setNum(commonCart.getNum()+num);
            commonCatService.updateById(commonCart);
            return Result.OK("修改成功");
        }else{
            CommonCat commonCat = new CommonCat();
            commonCat.setTitle(commonShop.getName());
            commonCat.setShopId(commonShop.getId());
            commonCat.setImage(commonShop.getImage());
            commonCat.setSku(commonShop.getMaterial());
            commonCat.setPrice(commonShop.getPrice());
            commonCat.setUserId(hanHaiMember.getId());
            commonCat.setNum(num);
            commonCat.setCreateTime(new Date());
            commonCatService.save(commonCat);
            return Result.OK("加入成功");
        }
    }

    //删除购物车信息
    @Override
    public Result<?> deleteCart(String token,String ids){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //删除多个购物车数据
        String[] split = ids.split(",");
        //如果没有,则直接删除
        if (split.length==1){
            commonCatService.removeById(split[0]);
            return Result.OK("删除成功");
        }
        //如果有则循环删除
        for (String id : split) {
            commonCatService.removeById(id);
        }
        return Result.OK("删除成功");
    }


    //修改购物车信息数量
    @Override
    public Result<?> updateCartNum(String token,String id,Integer num){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        CommonCat commonCat = commonCatService.getById(id);
        commonCat.setNum(num);
        commonCatService.updateById(commonCat);
        return Result.OK("修改成功");
    }



}
