//package com.gree.mall.miniapp.logic.user;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
//import com.gree.mall.miniapp.bean.user.ServiceWorkerBean;
//import com.gree.mall.miniapp.commonmapper.UserInfoMapper;
//import com.gree.mall.miniapp.constant.Constant;
//import com.gree.mall.miniapp.enums.ExchangeCodeTypeEnum;
//import com.gree.mall.miniapp.exception.RemoteServiceException;
//import com.gree.mall.miniapp.logic.common.CommonLogic;
//import com.gree.mall.miniapp.plus.entity.GiftExchangeCode;
//import com.gree.mall.miniapp.plus.entity.User;
//import com.gree.mall.miniapp.plus.service.GiftExchangeCodeService;
//import com.gree.mall.miniapp.plus.service.UserService;
//import com.gree.mall.miniapp.utils.DateUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.integration.redis.util.RedisLockRegistry;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.math.BigDecimal;
//import java.text.ParseException;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.locks.Lock;
//
///**
// * 兑换码业务处理
// */
//@Service
//public class ExchangeCodeLogic {
//
//    @Autowired
//    CommonLogic commonLogic;
//    @Autowired
//    RedisLockRegistry redisLockRegistry;
//    @Autowired
//    GiftExchangeCodeService giftExchangeCodeService;
//    @Autowired
//    UserService userService;
//    @Autowired
//    UserInfoMapper userInfoMapper;
//
//    /**
//     * 给师傅分配降价的兑换码
//     * @param amount
//     * @param day
//     * @param request
//     * @throws InterruptedException
//     * @throws ParseException
//     */
//    public void bindExchangeCode(BigDecimal amount,Integer day,HttpServletRequest request) throws InterruptedException, ParseException {
//
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//        //分享人
//        User user = userService.getById(currentCompanyWechat.getUserId());
//        //所属网点
//        ServiceWorkerBean serviceWorkerBean = userInfoMapper.queryWebsitByWorkId(user.getWorkUserId(),currentCompanyWechat.getCorpId());
//
//        Date endTime = DateUtils.getTimeByDay(day);
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_EXCHANGE_CODE + ExchangeCodeTypeEnum.SUB.toString());
//        if(!obtain.tryLock(5,TimeUnit.SECONDS)){
//            throw new RemoteServiceException("生成兑换码失败，系统繁忙请稍后再试");
//        }
//        try {
//            Integer count = giftExchangeCodeService.lambdaQuery()
//                    .eq(GiftExchangeCode::getUserId, user.getUserId())
//                    .eq(GiftExchangeCode::getStatus, 0)
//                    .eq(GiftExchangeCode::getType, ExchangeCodeTypeEnum.SUB.toString())
//                    .le(GiftExchangeCode::getStartTime, new Date())
//                    .ge(GiftExchangeCode::getEndTime, new Date())
//                    .count();
//            if(count > 10){
//                throw new RemoteServiceException("生成失败，监测到您已生成10个待使用的兑换码");
//            }
//
//            List<GiftExchangeCode> records = giftExchangeCodeService.lambdaQuery()
//                    .eq(GiftExchangeCode::getUserId, "")
//                    .eq(GiftExchangeCode::getType, ExchangeCodeTypeEnum.SUB.toString())
//                    .page(new Page<>(1, 1, false))
//                    .getRecords();
//            if (records.size() == 0) {
//                throw new RemoteServiceException("兑换码库存不足，请联系相关人员");
//            }
//            GiftExchangeCode giftExchangeCode = records.get(0);
//            giftExchangeCode.setUserId(currentCompanyWechat.getUserId());
//            giftExchangeCode.setNickName(user.getNickName());
//            giftExchangeCode.setUserPhone(user.getWorkPhone());
//            giftExchangeCode.setAvatar(user.getAvatar());
//            giftExchangeCode.setAmount(amount);
//            giftExchangeCode.setBindTime(new Date());
//            giftExchangeCode.setStartTime(new Date());
//            giftExchangeCode.setEndTime(endTime);
//            giftExchangeCode.setCompanyWechatId(currentCompanyWechat.getCompanyWechatId());
//            giftExchangeCode.setCompanyName(currentCompanyWechat.getCompanyName());
//            if (serviceWorkerBean != null) {
//                giftExchangeCode.setAdminWebsitId(serviceWorkerBean.getAdminWebsitId());
//                giftExchangeCode.setWebsitName(serviceWorkerBean.getWebsitNumber());
//                giftExchangeCode.setWebsitName(serviceWorkerBean.getWebsit());
//            }
//            giftExchangeCode.updateById();
//        }finally {
//            obtain.unlock();
//        }
//    }
//
//
//    /**
//     * 兑换码统计
//     */
//    public Map<String,Object> count(HttpServletRequest request){
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//        Integer ysy = giftExchangeCodeService.lambdaQuery()
//                .eq(GiftExchangeCode::getUserId, currentCompanyWechat.getUserId())
//                .le(GiftExchangeCode::getStartTime,new Date()).ge(GiftExchangeCode::getEndTime,new Date())
//                .eq(GiftExchangeCode::getStatus, 1).count();
//
//        Integer wsy = giftExchangeCodeService.lambdaQuery()
//                .eq(GiftExchangeCode::getUserId, currentCompanyWechat.getUserId())
//                .le(GiftExchangeCode::getStartTime,new Date()).ge(GiftExchangeCode::getEndTime,new Date())
//                .eq(GiftExchangeCode::getStatus, 0).count();
//
//        Map<String,Object> map = new HashMap<>();
//        map.put("ysy",ysy);
//        map.put("wsy",wsy);
//        map.put("all",ysy+wsy);
//        return map;
//    }
//
//
//    /**
//     * 兑换码列表
//     */
//    public IPage<GiftExchangeCode> list(Integer status,Integer pageNo,Integer pageSize,HttpServletRequest request){
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//        IPage<GiftExchangeCode> page = giftExchangeCodeService.lambdaQuery()
//                .eq(GiftExchangeCode::getType, ExchangeCodeTypeEnum.SUB.toString())
//                .eq(GiftExchangeCode::getUserId, currentCompanyWechat.getUserId())
//                .eq(status != null, GiftExchangeCode::getStatus, status)
//                .le(GiftExchangeCode::getStartTime,new Date()).ge(GiftExchangeCode::getEndTime,new Date())
//                .page(new Page<>(pageNo, pageSize));
//        return page;
//    }
//
//
//}
