package com.fs.his.service.impl;

import java.text.SimpleDateFormat;
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.his.domain.FsCoupon;
import com.fs.his.domain.FsUserCoupon;
import com.fs.his.mapper.FsUserCouponMapper;
import com.fs.his.param.FsCouponListUParam;
import com.fs.his.param.FsCouponParam;
import com.fs.his.param.FsCouponReceiveParam;
import com.fs.his.vo.FsCouponAllListVO;
import com.fs.his.vo.FsCouponListUVO;
import com.fs.his.vo.FsCouponListVO;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsCouponMapper;
import com.fs.his.service.IFsCouponService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券Service业务层处理
 *
 * @author fs
 * @date 2023-09-04
 */
@Service
public class FsCouponServiceImpl implements IFsCouponService
{
    @Autowired
    private FsCouponMapper fsCouponMapper;
    @Autowired
    private FsUserCouponMapper fsUserCouponMapper;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询优惠券
     *
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    @Override
    public FsCoupon selectFsCouponByCouponId(Long couponId)
    {
        return fsCouponMapper.selectFsCouponByCouponId(couponId);
    }

    /**
     * 查询优惠券列表
     *
     * @param fsCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<FsCoupon> selectFsCouponList(FsCoupon fsCoupon)
    {
        return fsCouponMapper.selectFsCouponList(fsCoupon);
    }

    /**
     * 新增优惠券
     *
     * @param fsCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertFsCoupon(FsCoupon fsCoupon)
    {
        fsCoupon.setCreateTime(DateUtils.getNowDate());
        fsCoupon.setRemainNumber(fsCoupon.getNumber());
        return fsCouponMapper.insertFsCoupon(fsCoupon);
    }

    /**
     * 修改优惠券
     *
     * @param fsCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateFsCoupon(FsCoupon fsCoupon)
        {
            fsCoupon.setUpdateTime(DateUtils.getNowDate());
            return fsCouponMapper.updateFsCoupon(fsCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param couponIds 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteFsCouponByCouponIds(Long[] couponIds)
    {
        return fsCouponMapper.deleteFsCouponByCouponIds(couponIds);
    }

    /**
     * 删除优惠券信息
     *
     * @param couponId 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteFsCouponByCouponId(Long couponId)
    {
        return fsCouponMapper.deleteFsCouponByCouponId(couponId);
    }

    @Override
    public List<FsCouponListVO> selectFsCouponListVO(FsCouponParam fsCoupon) {
        return fsCouponMapper.selectFsCouponListVO(fsCoupon);
    }

    @Override
    public List<FsCouponAllListVO> selectFsCouponListAll() {
        return fsCouponMapper.selectFsCouponListAll();
    }

    @Override
    public List<FsCouponListUVO> selectFsCouponListUVO(FsCouponListUParam param) {
        return fsCouponMapper.selectFsCouponListUVO(param);
    }

    @Override
    @Transactional
    public R receive(FsCouponReceiveParam param) {
        FsCoupon coupon=fsCouponMapper.selectFsCouponByCouponId(param.getCouponId());
        if(!coupon.getStatus().equals(1)){
            return R.error("此优惠券禁用");
        }
        if(coupon.getRemainNumber().equals(0l)){
            return R.error("此优惠券已领完");
        }
        if(fsUserCouponMapper.checkReceive(param.getUserId(),coupon.getCouponId())>0){
            return R.error("您已领取此券");
        }
        FsUserCoupon userCoupon=new FsUserCoupon();
        userCoupon.setUserId(param.getUserId());
        userCoupon.setStatus(0);
        userCoupon.setCouponId(coupon.getCouponId());
        userCoupon.setCouponCode(genCode());
        userCoupon.setCreateTime(new Date());
        userCoupon.setCompanyUserId(param.getCompanyUserId());
        userCoupon.setCompanyId(param.getCompanyId());
        if(fsUserCouponMapper.insertFsUserCoupon(userCoupon)>0){
            //更新剩余数量
            fsCouponMapper.updateRemainCount(coupon.getCouponId());
        }
        redisCache.deleteObject(param.getCode());
        return R.ok();
    }
    @Synchronized
    public static String genCode() {
        String year = new SimpleDateFormat("yy").format(new Date());
        String day = String.format("%tj", new Date());
        double random = Math.random() * 10000000;
        while (random < 1000000) {
            random = Math.random() * 10000000;
        }
        int intRandom = Double.valueOf(random).intValue();
        String verifyCode = year + day + intRandom;
        return verifyCode;
    }
}
