package com.fs.store.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.store.domain.*;
import com.fs.store.mapper.*;
import com.fs.store.param.FsCouponIssueParam;
import com.fs.store.param.FsStoreCouponReceiveParam;
import com.fs.store.vo.FsStoreCouponIssueVO;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.store.service.IFsStoreCouponIssueService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券领取Service业务层处理
 *
 * @author fs
 * @date 2022-03-15
 */
@Service
public class FsStoreCouponIssueServiceImpl implements IFsStoreCouponIssueService
{
    @Autowired
    RedisCache redisCache;
    @Autowired
    private FsStoreCouponMapper couponMapper;
    @Autowired
    private FsStoreCouponIssueMapper fsStoreCouponIssueMapper;
    @Autowired
    private FsStoreCouponIssueUserMapper fsStoreCouponIssueUserMapper;
    @Autowired
    private FsStoreCouponUserMapper fsStoreCouponUserMapper;
    @Autowired
    private FsUserMapper userMapper;
    /**
     * 查询优惠券领取
     *
     * @param id 优惠券领取ID
     * @return 优惠券领取
     */
    @Override
    public FsStoreCouponIssue selectFsStoreCouponIssueById(Long id)
    {
        return fsStoreCouponIssueMapper.selectFsStoreCouponIssueById(id);
    }

    /**
     * 查询优惠券领取列表
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 优惠券领取
     */
    @Override
    public List<FsStoreCouponIssue> selectFsStoreCouponIssueList(FsStoreCouponIssue fsStoreCouponIssue)
    {
        return fsStoreCouponIssueMapper.selectFsStoreCouponIssueList(fsStoreCouponIssue);
    }

    /**
     * 新增优惠券领取
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 结果
     */
    @Override
    public int insertFsStoreCouponIssue(FsStoreCouponIssue fsStoreCouponIssue)
    {
        fsStoreCouponIssue.setCreateTime(DateUtils.getNowDate());
        return fsStoreCouponIssueMapper.insertFsStoreCouponIssue(fsStoreCouponIssue);
    }

    /**
     * 修改优惠券领取
     *
     * @param fsStoreCouponIssue 优惠券领取
     * @return 结果
     */
    @Override
    public int updateFsStoreCouponIssue(FsStoreCouponIssue fsStoreCouponIssue)
    {
        fsStoreCouponIssue.setUpdateTime(DateUtils.getNowDate());
        return fsStoreCouponIssueMapper.updateFsStoreCouponIssue(fsStoreCouponIssue);
    }

    /**
     * 批量删除优惠券领取
     *
     * @param ids 需要删除的优惠券领取ID
     * @return 结果
     */
    @Override
    public int deleteFsStoreCouponIssueByIds(Long[] ids)
    {
        return fsStoreCouponIssueMapper.deleteFsStoreCouponIssueByIds(ids);
    }

    /**
     * 删除优惠券领取信息
     *
     * @param id 优惠券领取ID
     * @return 结果
     */
    @Override
    public int deleteFsStoreCouponIssueById(Long id)
    {
        return fsStoreCouponIssueMapper.deleteFsStoreCouponIssueById(id);
    }

    @Override
    public List<FsStoreCouponIssueVO> selectFsStoreCouponIssueListQueryVO(String userId, FsStoreCouponIssue map) {
        return fsStoreCouponIssueMapper.selectFsStoreCouponIssueListQueryVO(userId,map);
    }

    @Override
    @Synchronized
    @Transactional
    public R receive(String userId, FsStoreCouponReceiveParam param) {
        FsStoreCouponIssue couponIssue=fsStoreCouponIssueMapper.selectFsStoreCouponIssueById(param.getId());
        if(couponIssue.getStatus().equals(0)){
            return R.error("此优惠券已停止领取");
        }
        if(couponIssue.getLimitTime().getTime()<new Date().getTime()){
            return R.error("此优惠券已过期");
        }
        //判断用户是否领取过
        FsUser user= userMapper.selectFsUserById(Long.parseLong(userId));
        if(user.getLevel().equals(0)){
            if(fsStoreCouponIssueUserMapper.checkReceive(userId,param.getId())>0){
                return R.error("已领取");
            }
        }
        if(couponIssue.getTotalCount()<=couponIssue.getRemainCount()){
            return R.error("此优惠券已领完");
        }
        FsStoreCoupon coupon=couponMapper.selectFsStoreCouponById(couponIssue.getCouponId());
        //写入领取表
        FsStoreCouponIssueUser couponIssueUser=new FsStoreCouponIssueUser();
        couponIssueUser.setUserId(Long.parseLong(userId));
        couponIssueUser.setIssueId(param.getId());
        couponIssueUser.setIsDel(0);
        couponIssueUser.setCreateTime(new Date());
        fsStoreCouponIssueUserMapper.insertFsStoreCouponIssueUser(couponIssueUser);
        FsStoreCouponUser couponUser=new FsStoreCouponUser();
        couponUser.setCouponId(couponIssue.getCouponId());
        couponUser.setUserId(Long.parseLong(userId));
        couponUser.setCouponTitle(couponIssue.getCouponName());
        couponUser.setCouponPrice(coupon.getCouponPrice());
        couponUser.setUseMinPrice(coupon.getUseMinPrice());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,coupon.getCouponTime().intValue());
        couponUser.setLimitTime(calendar.getTime());
        couponUser.setCreateTime(new Date());
        couponUser.setType("send");
        couponUser.setStatus(0);
        fsStoreCouponUserMapper.insertFsStoreCouponUser(couponUser);
        //更新领取数量
        fsStoreCouponIssueMapper.updateFsStoreCouponIssueCount(param.getId());
        return R.ok("领取成功");
    }

    @Override
    public List<FsStoreCouponIssueVO> getCompanyCouponIssueList(FsCouponIssueParam map) {
        return fsStoreCouponIssueMapper.getCompanyCouponIssueList(map);
    }

    @Override
    public FsStoreCouponIssueVO selectFsStoreCouponIssueVOById(Long id) {
        return fsStoreCouponIssueMapper.selectFsStoreCouponIssueVOById(id);
    }

    @Override
    public List<FsStoreCouponIssueVO> selectFsStoreCouponIssueListVO(FsStoreCouponIssue fsStoreCouponIssue) {
        return fsStoreCouponIssueMapper.selectFsStoreCouponIssueListVO(fsStoreCouponIssue);
    }


}
