package com.ssm.manage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmCollectionTypeDao;
import com.ssm.manage.entity.BaseCollectionType;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.CollectionTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCollectionTypeService;
import com.ssm.util.RedisClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class SsmCollectionTypeServiceImpl extends BaseServiceImpl<BaseCollectionType> implements SsmCollectionTypeService {
    @Autowired
    private RedisClient<List<BaseCollectionType>> redisClient;
    @Autowired
    private SsmCollectionTypeDao dao;

    @Override
    public SsmCollectionTypeDao getDao() {
        return dao;
    }

    private static String key = "collectionTypeList";

    @Override
    public PageInfo<BaseCollectionType> selectPage(CollectionTypeRequest collectionTypeRequest) {
        return dao.selectPage(collectionTypeRequest);
    }

    @Override
    public int saveCollectionType(CollectionTypeRequest collectionTypeRequest) throws SmsException {
        //校验支付方式编码是否已存在
        getCollectionType(collectionTypeRequest.getCollectionType(),null);
        //校验支付方式名称是否已存在
        getCollectionType(null,collectionTypeRequest.getCollectionTypeName());
        BaseCollectionType collectionType = new BaseCollectionType();
        //设置主键
        collectionType.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        collectionType.setCollectionType(collectionTypeRequest.getCollectionType());
        collectionType.setCollectionTypeName(collectionTypeRequest.getCollectionTypeName());
        collectionType.setState(1);
        int i = dao.saveNotNull(collectionType);
        redisClient.delete(key);
        return i;
    }

    @Override
    public int updateCollectionType(CollectionTypeRequest collectionTypeRequest) throws SmsException {
        //校验支付方式编码是否已存在
        getCollectionType(collectionTypeRequest.getCollectionType(),null);
        //校验支付方式名称是否已存在
        getCollectionType(null,collectionTypeRequest.getCollectionTypeName());
        BaseCollectionType collectionType = new BaseCollectionType();
        collectionType.setId(collectionTypeRequest.getId());
        collectionType.setCollectionTypeName(collectionTypeRequest.getCollectionTypeName());
        collectionType.setState(1);
        int i = dao.updateByPrimaryKeySelective(collectionType);
        redisClient.delete(key);
        return i;
    }

    @Override
    public int updateCollectionTypeState(CollectionTypeRequest collectionTypeRequest) {
        Example example = new Example(BaseCollectionType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",collectionTypeRequest.getId());
        BaseCollectionType collectionType = new BaseCollectionType();
        collectionType.setState(collectionTypeRequest.getState());
        int i = dao.updateByExampleSelective(collectionType,example);
        redisClient.delete(key);
        return i;
    }

    @Override
    public List<BaseCollectionType> selectList() {
        List<BaseCollectionType> list = new ArrayList<>();
       /* if (redisClient.hasKey(key)){
            list = redisClient.get(key);
        }else {*/
            BaseCollectionType collectionType = new BaseCollectionType();
            collectionType.setState(1);
            list = dao.select(collectionType);
            redisClient.set(key,list,RedisClient.EXPIRE_TIME_ONE_DAY);
        //}
        return list;
    }

    public void getCollectionType(String collectType, String collectionTypeName) throws SmsException {
        BaseCollectionType baseCollectionType = new BaseCollectionType();

        if (collectType != null) {
            baseCollectionType.setCollectionType(collectType);
            baseCollectionType = selectOne(baseCollectionType);
            if (baseCollectionType != null){
                throw new SmsException(ResponseCode.DATA_ALREADY_EXISTS_S,"支付方式编码");
            }
        }else if (collectionTypeName != null) {
            baseCollectionType.setCollectionTypeName(collectionTypeName);
            baseCollectionType = selectOne(baseCollectionType);
            if (baseCollectionType != null){
                throw new SmsException(ResponseCode.DATA_ALREADY_EXISTS_S,"支付方式名称");
            }
        }
    }
}
