package com.pj.tb_goods_cart;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pj.common.core.exception.ServiceException;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.IsLock;
import com.pj.enummj.UserType;
import com.pj.tb_goods_cart.dto.GoodsCartInfoDto;
import com.pj.tb_goods_cart.vo.GoodsCartVo;
import com.pj.tb_goods_transit.TbGoodsTransit;
import com.pj.tb_goods_transit.TbGoodsTransitMapper;
import com.pj.tb_people.TbPeople;
import com.pj.tb_people.TbPeopleMapper;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.pj.utils.sg.*;

/**
 * Service: tb_goods_cart -- 购物车
 * @author qzy 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbGoodsCartService extends ServiceImpl<TbGoodsCartMapper, TbGoodsCart> implements IService<TbGoodsCart>{

	/** 底层 Mapper 对象 */
	@Autowired
	TbGoodsCartMapper tbGoodsCartMapper;

	@Autowired
	private MethodGoodsCartService methodGoodsCartService;
	/** 已过审的商品 */
	@Autowired
	private TbGoodsTransitMapper tbGoodsTransitMapper;
	@Autowired
	private TbPeopleMapper tbPeopleMapper;
	/** 增 */
	void add(TbGoodsCart t){
		save(t);
	}

	/** 删 */
	void delete(Long id){
		removeById(id);
	}

	/** 改 */
	void update(TbGoodsCart t){
		updateById(t);

	}

	/** 查 */
	TbGoodsCart getById(String id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	List<GoodsCartVo> getList(SoMap so) {
		//检查登录
		APPLoginUserInfo appUser = StpAPPUserUtil.getAPPLoginInfo();
		if(appUser == null || appUser.getLoginId() == null)throw new ServiceException("登陆账号信息异常!");
		//查看自己的购物车
		so.put("buyUserId",appUser.getLoginId());
		so.put("deleteStatus",DeleteStatus.DELETE_STATUS_ON.getCode());
		List<TbGoodsCart> cartList = tbGoodsCartMapper.getList(so);
		List<GoodsCartVo> goodsCartVos = methodGoodsCartService.changeTbGoodsCartToGoodsCartVoList(cartList);
		return goodsCartVos;
	}

	/** 添加商品进购物车 */
	public AjaxJson addGoodsInShopCart(GoodsCartInfoDto goodsCartInfoDto){
		//检查登录
		APPLoginUserInfo appUser = StpAPPUserUtil.getAPPLoginInfo();
		if(appUser == null || appUser.getLoginId() == null)return AjaxJson.getError("购买者账号异常");
		//判断当前用户是否为一级市场边民组长
		int leader = tbPeopleMapper.selectList(new LambdaQueryWrapper<TbPeople>()
						.eq(TbPeople::getId, appUser.getFk())
						.eq(TbPeople::getRole, 2)
						.eq(TbPeople::getIsLock, IsLock.IS_LOCK_ON.getCode())
						.eq(TbPeople::getDeleteStatus, DeleteStatus.DELETE_STATUS_ON.getCode()))
						.size();
		if(leader == 0)throw new ServiceException("您当前的身份不是互市组组长,无法添加购物车!");
		//购物车查重
		int size = tbGoodsCartMapper.selectList(new LambdaQueryWrapper<TbGoodsCart>()
				.eq(TbGoodsCart::getBuyUserId, appUser.getLoginId()).eq(TbGoodsCart::getPublishGoodsId,
						goodsCartInfoDto.getPublishGoodsId()).eq(TbGoodsCart::getDeleteStatus,
						DeleteStatus.DELETE_STATUS_ON.getCode())).size();
		if(size != 0)
			throw new ServiceException("当前商品已存在您的购物车中,请勿重复添加");
		//获取商品对象
		Long publishGoodsId = goodsCartInfoDto.getPublishGoodsId();
		TbGoodsTransit tbGoodsTransit = tbGoodsTransitMapper.selectById(publishGoodsId);
		if(tbGoodsTransit == null)return AjaxJson.getError("当前商品信息存在异常");
		//创建保存对象
		TbGoodsCart tbGoodsCart = new TbGoodsCart();
		BeanUtils.copyProperties(goodsCartInfoDto,tbGoodsCart);
		//购买的用户类型
		tbGoodsCart.setBuyType(appUser.getUserType());
		//购买方式,默认都是互助组进行购买
		tbGoodsCart.setBuyType(UserType.USER_TYPE_GROUP_LEADER.getCode());
		//购买商品的重量
		tbGoodsCart.setBuyWeight(goodsCartInfoDto.getBuyWeight());
		//当前商品的单位
		tbGoodsCart.setGoodsUnit(tbGoodsTransit.getGoodsUnits());
		//goodsTransitId
		tbGoodsCart.setPublishGoodsId(publishGoodsId);
		//tbGoodsId
		tbGoodsCart.setSaleGoodsInfoId(tbGoodsTransit.getGoodsId());
		//商品价格
		tbGoodsCart.setTotalPrice(tbGoodsTransit.getPrice());
		//设置基本属性
		tbGoodsCart.setCreateTime(new Date());
		tbGoodsCart.setDeleteStatus(DeleteStatus.DELETE_STATUS_ON.getCode());
		tbGoodsCart.setCreateBy(appUser.getLoginId().toString());
		tbGoodsCart.setCreateName(appUser.getLoginName());
		//执行保存
		int insert = tbGoodsCartMapper.insert(tbGoodsCart);
		//判断保存结果
		if(insert != 1)throw new RuntimeException("添加购物车失败!");
		return AjaxJson.getSuccess("添加购物车成功!");
	}

	/** 删除购物车,可批量 */
	public int cleanCart(List<Long> ids){
		if(ids.size() == 0)return 0;
		//清理购物车
		int batchIds = tbGoodsCartMapper.deleteBatchIds(ids);
		System.out.println("已清空 " + batchIds + " 个商品!");
		return batchIds;
	}

	/**
	 * todo:采购支付,暂时预留
	 */
	




}
