package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.ShopcarService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.teambuyBuycar.entity.TeambuyBuycar;
import org.jeecg.modules.teambuyBuycar.service.ITeambuyBuycarService;
import org.jeecg.modules.teambuyGoods.entity.TeambuyGoods;
import org.jeecg.modules.teambuyGoods.service.ITeambuyGoodsService;
import org.jeecg.modules.teambuyOrderGoods.entity.TeambuyOrderGoods;
import org.jeecg.modules.teambuyOrderGoods.service.ITeambuyOrderGoodsService;
import org.jeecg.modules.teambuyOrderLeader.service.ITeambuyOrderLeaderService;
import org.jeecg.modules.teambuyOrderMember.entity.TeambuyOrderMember;
import org.jeecg.modules.teambuyOrderMember.service.ITeambuyOrderMemberService;
import org.springframework.stereotype.Service;

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

@Service
public class ShopcarServiceImpl implements ShopcarService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //购物车信息
    @Resource
    private ITeambuyBuycarService teambuyBuycarService;
    //商品信息
    @Resource
    private ITeambuyGoodsService teambuyGoodsService;
    /******************************************************************************************************************/
    //查看购物车列表
    @Override
    public Result<?> queryShopcarList(String token, TeambuyBuycar teambuyBuycar, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1898983659468853250");
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyBuycar> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyBuycar> query = null;
        //返回信息
        Page<TeambuyBuycar> pageList = null;

        try{
            //分页
            page = new Page<TeambuyBuycar>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyBuycarService
                    .lambdaQuery();

            //组装查询条件
            if(StringUtils.isNotBlank(teambuyBuycar.getId())){
                //购物车编号
                query.eq(TeambuyBuycar::getId, teambuyBuycar.getId());
            }
            //购物车所属
            query.eq(TeambuyBuycar::getUserId, hanHaiMember.getId());
            //按照创建时间降序排列
            query.orderByDesc(TeambuyBuycar::getCreateTime);

            //获取购物车信息列表
            pageList = query.page(page);

            //查询购物车商品信息
            for (TeambuyBuycar record : pageList.getRecords()) {
                TeambuyGoods goods = teambuyGoodsService.getById(record.getGoodsId());
                record.setGoods(goods);
            }

            return Result.OK("购物车列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("购物车列表查询失败");
        }
    }

    //添加购物车
    @Override
    public Result<?> addShopcar(String token, TeambuyBuycar teambuyBuycar) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919589727643111426");
        //返回信息
        String massege = "";

        try{
            //判断该用户添加的商品是否存在，存在则进行数量增加
            TeambuyBuycar buycar = teambuyBuycarService
                    .lambdaQuery()
                    .eq(TeambuyBuycar::getUserId, hanHaiMember.getId())
                    .eq(TeambuyBuycar::getGoodsId, teambuyBuycar.getGoodsId())
                    .one();
            if(null != buycar){
                int num = buycar.getNum() + teambuyBuycar.getNum();
                teambuyBuycar.setNum(num);
                teambuyBuycar.setId(buycar.getId());
            }

            //执行新增操作(不传id进行新增，传了id则进行修改)
            teambuyBuycar.setUserId(hanHaiMember.getId());
            boolean result = teambuyBuycarService.saveOrUpdate(teambuyBuycar);

            //判断是否新增成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.OK("操作失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    //取消购物车
    @Override
    public Result<?> deleteShopcar(String token, String shopcarId) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String massege = "";

        try{

            String[] shopcarIds = shopcarId.split(";");

            for (String id : shopcarIds) {
                //判断该用户购物车商品是否存在
                TeambuyBuycar buycar = teambuyBuycarService
                        .lambdaQuery()
                        .eq(TeambuyBuycar::getUserId, hanHaiMember.getId())
                        .eq(TeambuyBuycar::getId, id)
                        .one();
                if(null == buycar){
                    return Result.error("该用户购物车没有该商品信息，请检查购物车id"+id);
                }
            }

            //执行新增操作(不传id进行新增，传了id则进行修改)
            //boolean result = teambuyBuycarService.removeById(shopcarId);
            boolean result = teambuyBuycarService.removeByIds(Arrays.asList(shopcarIds));

            //判断是否新增成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.OK("操作失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

}
