package com.iamberry.wechat.service.thanksgiving;

import com.iamberry.wechat.core.entity.coupon.CouponItem;
import com.iamberry.wechat.core.entity.coupon.CouponItemDto;
import com.iamberry.wechat.core.entity.coupon.CouponType;
import com.iamberry.wechat.core.entity.giftCard.UserMachineInfo;
import com.iamberry.wechat.core.entity.thanksgiving.ThanksGiving;
import com.iamberry.wechat.face.thanksgiving.ThanksGivingService;
import com.iamberry.wechat.service.mapper.CouponItemMapper;
import com.iamberry.wechat.service.mapper.CouponTypeMapper;
import com.iamberry.wechat.service.mapper.MemberMapper;
import com.iamberry.wechat.service.mapper.ThanksGivingMapper;
import com.iamberry.wechat.tools.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 *  活动-领取优惠券接口
 * @author
 * @Date 2018-12-11
 */
@Service
public class ThanksGivingServiceImpl  implements ThanksGivingService {
    @Autowired
    private ThanksGivingMapper thanksGivingMapper;
    @Autowired
    private CouponItemMapper couponItemMapper;
    @Autowired
    private CouponTypeMapper couponTypeMapper;
    @Autowired
    private MemberMapper memberMapper;
    /**
     * 获取集合
     * @param  thanksGiving
     * @return List
     */
    @Override
    public List<ThanksGiving> getThanksGivingList(ThanksGiving  thanksGiving){
        return  thanksGivingMapper.getThanksGivingList(thanksGiving);
    }
    /**
     * 查询单条数据
     * @param  id
     * @return  thanksGiving
     */
    @Override
    public  ThanksGiving  getThanksGivingById(Integer  id){
        return  thanksGivingMapper.getThanksGivingById(id);
    }

    @Override
    public ThanksGiving getThanksGivingByPhone(String phone) {
        return  thanksGivingMapper.getThanksGivingByPhone(phone);
    }

    /**
     * 增加数据
     * @param  thanksGiving
     * @return Integer
     */
    @Override
    public  Integer  save(ThanksGiving  thanksGiving){
        return  thanksGivingMapper.save(thanksGiving);
    }
    /**
     * 修改数据
     * @param  thanksGiving
     * @return Integer
     */
    @Override
    @Transactional
    public  Integer  update(ThanksGiving  thanksGiving){
        Integer couponId = 10000;
        CouponType couponType = couponTypeMapper.getCouponTypeById(couponId);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 90);  //有效期三个月

        CouponItemDto couponItemDto = new CouponItemDto();
        couponItemDto.setUseropenid(thanksGiving.getThanksGivingOpenId());
        couponItemDto.setCouponId(couponId);
        List<CouponItemDto> couponItemDtoList = couponItemMapper.getCouponItemDtoList(couponItemDto);
        if(couponItemDtoList != null && couponItemDtoList.size() >= couponType.getCouponGetLimit()){
            throw new RuntimeException("该用户已经领取过优惠券了");
        }

        //创建优惠券
        CouponItem couponItem = new CouponItem();
        String uuidStr = StrUtils.getUUID();
        couponItem.setCouponItemId(uuidStr);
        couponItem.setCouponItemUseropenid(thanksGiving.getThanksGivingOpenId());
        couponItem.setCouponId(couponType.getCouponId());
        couponItem.setCouponReceiveDate(new Date());
        couponItem.setCouponUseEndDate(calendar.getTime());
        couponItem.setCouponUseStatus(1);
        couponItem.setCouponItemRemark(couponType.getCouponRemark());
        Integer flag = couponItemMapper.insertCouponItem(couponItem);
        if(flag < 1){
            throw new RuntimeException("创建优惠券失败");
        }

        //修改领取状态
        thanksGiving.setCouponItemId(uuidStr);
        thanksGiving.setThanksGivingStatus(2);
        thanksGiving.setThanksGivingTime(new Date());
        flag = thanksGivingMapper.update(thanksGiving);
        if(flag < 1){
            throw new RuntimeException("修改领取状态失败");
        }

        //修改会员信息，变成vip会员
        flag = memberMapper.updateUserIsVip(thanksGiving.getThanksGivingOpenId());
        if(flag < 1){
            throw new RuntimeException("注册成为VIP会员状态失败");
        }
        return  flag;
    }

    /**
     * 删除数据
     * @param  id
     * @return Integer
     */
    @Override
    public  Integer  delete(Integer  id){
        return  thanksGivingMapper.delete(id);
    }
}