package com.huiju.estateagents.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaGoods;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaPointsExchange;
import com.huiju.estateagents.entity.TaPointsRecords;
import com.huiju.estateagents.mapper.TaGoodsMapper;
import com.huiju.estateagents.mapper.TaPersonMapper;
import com.huiju.estateagents.mapper.TaPointsExchangeMapper;
import com.huiju.estateagents.mapper.TaPointsRecordsMapper;
import com.huiju.estateagents.service.ITaGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 商品表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TaGoodsServiceImpl extends ServiceImpl<TaGoodsMapper, TaGoods> implements ITaGoodsService {
	
	@Autowired
	private TaPersonMapper taPersonMapper;
	
	@Autowired
	private TaGoodsMapper taGoodsMapper;
	
	@Autowired
	private TaPointsRecordsMapper taPointsRecordsMapper;
	
	@Autowired
	private TaPointsExchangeMapper taPointsExchangeMapper;
	
	/**
	 * 兑换商品
	 *
	 * @param id
	 * @param openid
	 * @return
	 */
	@Override
	public String exchangeGoods(Integer id, String openid) {
		//插入积分消耗流水,要查看积分是否足够兑换
		TaPerson taPerson = taPersonMapper.getPersonByOpenId(openid).get(0);
		if (null == taPerson.getPoints()) {
			taPerson.setPoints(0);
		}

		TaGoods taGoods = taGoodsMapper.selectById(id);
		if (null == taGoods.getPointPrice()) {
			taGoods.setPointPrice(0);
		}

		//判断积分是否足够
		if (null == taPerson.getPoints() || taPerson.getPoints().intValue() < taGoods.getPointPrice()){
			return "积分不足，请先获取积分";
		}
		//判断商品是否够兑换
		if (null == taGoods.getInventory()|| taGoods.getInventory().intValue() < 1){
			return "此商品无库存";
		}

		taGoods.setExchanger(taPerson);

		//插入积分消费流水表
		insertTaPointRecords(taPerson,taGoods);
		//插入积分兑换记录
		insertTaPointsExchange(taPerson,taGoods);
		//扣除积分重新更新persion表
		Integer remainingPoints = taPerson.getPoints() - taGoods.getPointPrice();
		taPerson.setPoints(remainingPoints);
		taPersonMapper.updateById(taPerson);
		//商品数减少
		taGoods.setInventory(taGoods.getInventory() -1);
		taGoodsMapper.updateById(taGoods);
		return CommConstant.SUCCESS;
	}
	
	/**
	 * 插入积分兑换记录
	 * @param taPerson
	 * @param taGoods
	 */
	private void insertTaPointsExchange(TaPerson taPerson, TaGoods taGoods) {
		TaPointsExchange taPointsExchange = new TaPointsExchange();
		taPointsExchange.setPersonId(taPerson.getPersonId());
		taPointsExchange.setPersonName(StringUtils.ifNull(taPerson.getName(),taPerson.getNickname()));

		// 存在 phone 字段没有值的情况，但是 tel 字段有值
		String phone = org.apache.commons.lang3.StringUtils.isNotBlank(taPerson.getPhone()) ? taPerson.getPhone() : taPerson.getTel();
		taPointsExchange.setPhone(phone);
		taPointsExchange.setAvatar(taPerson.getAvatarurl());
		taPointsExchange.setTargetId(taGoods.getGoodsId().toString());
		taPointsExchange.setTargetName(taGoods.getGoodsName());
		taPointsExchange.setImage(taGoods.getImgUrl());
		taPointsExchange.setPoints(taGoods.getPointPrice());
		taPointsExchange.setAddress(taGoods.getAddress());
		//0 未领取 1 已领取
		taPointsExchange.setStatus(CommConstant.STATUS_UNACCALIMED);
		taPointsExchange.setCreateDate(LocalDateTime.now());
		taPointsExchange.setOrgId(taGoods.getOrgId());
		taPointsExchange.setBuildingId(taGoods.getBuildingId());
		taPointsExchangeMapper.insert(taPointsExchange);
	}
	
	/**
	 * 插入积分消费流水表
	 * @param taPerson
	 * @param taGoods
	 */
	private void insertTaPointRecords(TaPerson taPerson,TaGoods taGoods) {
		TaPointsRecords taPointsRecords = new TaPointsRecords();
		taPointsRecords.setPersonId(taPerson.getPersonId());
		taPointsRecords.setPersonName(StringUtils.ifNull(taPerson.getName(),taPerson.getNickname()));
		taPointsRecords.setPersonType(taPerson.getPersonType());
		taPointsRecords.setPointsAmount(-taGoods.getPointPrice());
		taPointsRecords.setChangeType(CommConstant.POINTS_CHANGE_GOODS);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("goods_id",taGoods.getGoodsId());
		taPointsRecords.setChangeParams(jsonObject.toJSONString());
		taPointsRecords.setCreateDate(LocalDateTime.now());
		taPointsRecords.setOrgId(taGoods.getOrgId());
		taPointsRecords.setBuildingId(taGoods.getBuildingId());
		taPointsRecords.setStatus(CommConstant.STATUS_NORMAL);
		taPointsRecordsMapper.insert(taPointsRecords);
	}
}
