package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.mapper.SmsRechargeListMapper;
import com.ssm.manage.mapper.SmsTypeMapper;
import com.ssm.manage.query.SmsTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.SmsBalanceService;
import com.ssm.manage.service.SmsTypeService;
import com.ssm.manage.utils.SnowflakeIdUtils;
import com.ssm.util.RedisClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SmsTypeServiceImpl extends BaseServiceImpl<SmsType> implements SmsTypeService {

    @Autowired
    private SmsTypeMapper smsTypeMapper;

    @Autowired
    private SmsRechargeListMapper smsRechargeListMapper;

    @Autowired
    private RedisClient<UserResponse> userRedisClient;
    @Autowired
    private RedisClient<StaffResponse> staffRedisClient;

    private static String userKey = "userKey";
    private static String staffKey = "staffKey";
    @Override
    public BaseDao<SmsType> getDao() {
        return null;
    }

    @Override
    public int saveSmsType(SmsType smsType,String phone) {
        //获取登录人的信息
        if (userRedisClient.hasKey(getUserKey(phone))) {
            UserResponse staffResponse = userRedisClient.get(getUserKey(phone));
            smsType.setInputStaff(staffResponse.getUserName());
            smsType.setInputDate(new Date());
        }
        if(smsType.getType()==1) {
            BigDecimal number = new BigDecimal(smsType.getSmsNumber() + smsType.getGiftNumber());
            BigDecimal smsPrice = smsType.getRechargeMoney().divide(number, 4, BigDecimal.ROUND_HALF_UP);
            smsType.setSmsPrice(smsPrice);
        }
        if(smsType.getType()==2){
            smsType.setRechargeMoney(null);
            smsType.setSmsNumber(null);
            smsType.setGiftNumber(null);
        }
        smsType.setState(0);
        smsType.setId(getId());
        if(smsType.getStopDate()==null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = format.parse("2099-01-01 23:59:59");
                smsType.setStopDate(parse);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        int insert = smsTypeMapper.insert(smsType);
        return insert;
    }

    @Override
    public PageInfo<SmsType> selectBystoreId(SmsTypeRequest smsType) {
        SmsType type = new SmsType();
        BeanUtils.copyProperties(smsType,type);
        PageHelper.startPage(smsType.getPageNum(),smsType.getPageSize(),"state desc,sms_type desc,id desc");
        List<SmsType> select = smsTypeMapper.select(type);
        PageInfo<SmsType> pageInfo = new PageInfo<>(select);
        return pageInfo;
    }

    @Override

    public int updateBySmsType(SmsType smsType,String phone) throws SmsException {
        //判断是否有交易发生
        SmsRechargeList rechargeList = new SmsRechargeList();
        rechargeList.setRechargeTypeId(smsType.getId());
        List<SmsRechargeList> select = smsRechargeListMapper.select(rechargeList);
        if(select!=null && select.size()>0){
            throw new SmsException(ResponseCode.MSG,"已发生交易，不允许修改");
        }
        //获取登录人的信息
        if (userRedisClient.hasKey(getUserKey(phone))) {
            UserResponse userResponse = userRedisClient.get(getUserKey(phone));
            smsType.setUpdateStaff(userResponse.getUserName());
            smsType.setUpdateDate(new Date());
        }
        if(smsType.getType()==1) {
            BigDecimal number = new BigDecimal(smsType.getSmsNumber() + smsType.getGiftNumber());
            BigDecimal smsPrice = smsType.getRechargeMoney().divide(number, 4, BigDecimal.ROUND_HALF_UP);
            smsType.setSmsPrice(smsPrice);
        }
        if(smsType.getType()==2){
            smsType.setRechargeMoney(null);
            smsType.setSmsNumber(null);
            smsType.setGiftNumber(null);
        }
        int insert = smsTypeMapper.updateByType(smsType);
        return insert;
    }

    @Override
    public int deleteById(Long id) throws SmsException {
        //判断是否有交易发生
        SmsRechargeList rechargeList = new SmsRechargeList();
        rechargeList.setRechargeTypeId(id);
        List<SmsRechargeList> select = smsRechargeListMapper.select(rechargeList);
        if(select!=null && select.size()>0){
            throw new SmsException(ResponseCode.MSG,"已发生交易，不允许删除");
        }
        int insert = smsTypeMapper.deleteById(id);
        return insert;
    }

    @Override
    public int updateState(SmsType smsType, String loginAccount) {
        //获取登录人的信息
        if (userRedisClient.hasKey(getUserKey(loginAccount))) {
            UserResponse staffResponse = userRedisClient.get(getUserKey(loginAccount));
            smsType.setUpdateStaff(staffResponse.getUserName());
            smsType.setUpdateDate(new Date());
        }

        int insert = smsTypeMapper.updateState(smsType);
        return insert;
    }


    public String getUserKey(String phone){
        return userKey + phone;
    }

    public Long getId(){
        return new SnowflakeUtil(new Random().nextInt(1023)).nextId();
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }
}
