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.SsmStoreMemberClassDao;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreMemberClass;
import com.ssm.manage.entity.StoreMemberList;
import com.ssm.manage.entity.StoreRoom;
import com.ssm.manage.query.StoreMemberClassRequest;
import com.ssm.manage.query.StoreRoomRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.SsmStoreMemberClassService;
import com.ssm.manage.service.SsmStoreMemberService;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
@Slf4j
public class ssmStoreMemberClassServiceImpl  extends BaseServiceImpl<StoreMemberClass> implements SsmStoreMemberClassService {

    private static String staffKey = "staffKey";

    @Autowired
    private SsmStoreMemberClassDao dao;
    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private RedisClient<StaffResponse> redisClient;

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

    @Override
    public PageInfo<StoreMemberClass> selectPage(StoreMemberClassRequest storeRoomRequest){
        Example example = new Example(StoreRoom.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("storeId",storeRoomRequest.getStoreId());
        if (null != storeRoomRequest.getClassNum()){
            criteria.andLike("classNum","%" + storeRoomRequest.getClassNum() + "%");
        }
        if (null != storeRoomRequest.getClassName()){
            criteria.andLike("className","%" + storeRoomRequest.getClassName() + "%");
        }
        if (null != storeRoomRequest.getState()){
            criteria.andEqualTo("state",storeRoomRequest.getState());
        }
//        storeRoomRequest.setSort("state");
        PageHelper.startPage(storeRoomRequest.getPageNum(),storeRoomRequest.getPageSize(),"state desc,class_row desc");
        List<StoreMemberClass> list = selectByExample(example);
        PageInfo<StoreMemberClass> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(StoreMemberClassRequest storeMemberClassRequest, String phone) throws SmsException {

        StoreMemberClass storeMemberClass = new StoreMemberClass();

        storeMemberClass.setStoreId(storeMemberClassRequest.getStoreId());
        storeMemberClass.setClassNum(storeMemberClassRequest.getClassNum());
        List<StoreMemberClass> select = dao.select(storeMemberClass);
        if(select.size()>0){
            throw new SmsException(ResponseCode.MSG,"编码已经存在");
        }
        storeMemberClass.setClassNum(null);
        storeMemberClass.setClassName(storeMemberClassRequest.getClassName());
        List<StoreMemberClass> select1 = dao.select(storeMemberClass);
        if(select1.size()>0){
            throw new SmsException(ResponseCode.MSG,"名称已经存在");
        }

//        storeMemberClass.setClassNum(null);
        storeMemberClass.setClassName(null);
        storeMemberClass.setClassRow(storeMemberClassRequest.getClassRow());
        List<StoreMemberClass> select2 = dao.select(storeMemberClass);
        if(select2.size()>0){
            throw new SmsException(ResponseCode.MSG,"等级级别"+storeMemberClassRequest.getClassRow()+"已经存在");
        }
        BeanUtils.copyProperties(storeMemberClassRequest,storeMemberClass);
        storeMemberClass.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        storeMemberClass.setInputId(staffResponse.getId());
        storeMemberClass.setInputBy(staffResponse.getStaffName());
        storeMemberClass.setInputDate(new Date());
        storeMemberClass.setState(0);
        return dao.insert(storeMemberClass);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(StoreMemberClassRequest storeMemberClassRequest, String phone) throws SmsException {
        StoreMemberClass storeMemberClass = new StoreMemberClass();
        storeMemberClass.setId(storeMemberClassRequest.getId());
        StoreMemberClass storeMemberClass1 = dao.selectByPrimaryKey(storeMemberClass.getId());
        storeMemberClass.setId(null);
        storeMemberClass.setStoreId(storeMemberClassRequest.getStoreId());
        storeMemberClass.setClassNum(storeMemberClassRequest.getClassNum());
        StoreMemberClass storeMemberClass2 = dao.selectOne(storeMemberClass);
        if(null != storeMemberClass2){
            if(!storeMemberClass1.getId().equals(storeMemberClass2.getId())){
                throw new SmsException(ResponseCode.MSG,"编码已经存在");
            }
        }
        storeMemberClass.setClassNum(null);
        storeMemberClass.setClassName(storeMemberClassRequest.getClassName());
        StoreMemberClass storeMemberClass3 = dao.selectOne(storeMemberClass);
        if(null != storeMemberClass3){
            if(!storeMemberClass1.getId().equals(storeMemberClass3.getId())){
                throw new SmsException(ResponseCode.MSG,"名称已经存在");
            }
        }
        storeMemberClass.setClassName(null);
        storeMemberClass.setClassRow(storeMemberClassRequest.getClassRow());
        StoreMemberClass select2 = dao.selectOne(storeMemberClass);
        if(null != select2){
            if(!storeMemberClass1.getId().equals(select2.getId())){
                throw new SmsException(ResponseCode.MSG,"等级级别"+storeMemberClassRequest.getClassRow()+"已经存在");
            }
        }
        if(!storeMemberClass1.getClassNum().equals(storeMemberClass.getClassNum()) || !storeMemberClass1.getClassName().equals(storeMemberClass.getClassName()) ){
            StoreMemberList storeMemberList = new StoreMemberList();
            storeMemberList.setClassId(storeMemberClass1.getId());
            List<StoreMemberList> select = ssmStoreMemberService.select(storeMemberList);
            if(select.size()==0){
                storeMemberClass1.setClassNum(storeMemberClassRequest.getClassNum());
                storeMemberClass1.setClassName(storeMemberClassRequest.getClassName());
            }else{
                throw new SmsException(ResponseCode.MSG,"修改失败，该等级信息有会员关联");
            }
        }

//        if(storeMemberClass1.getState() != storeMemberClass.getState()){
//            StoreMemberList storeMemberList = new StoreMemberList();
//            storeMemberList.setClassId(storeMemberClass1.getId());
//            List<StoreMemberList> select = ssmStoreMemberService.select(storeMemberList);
//            if(select.size()==0){
//                storeMemberClass1.setState(storeMemberClassRequest.getState());
//            }else{
//                throw new SmsException(ResponseCode.MSG,"修改失败，该等级信息有会员关联");
//            }
//        }

        BeanUtils.copyProperties(storeMemberClassRequest,storeMemberClass1);
        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        storeMemberClass.setUpdateId(staffResponse.getId());
        storeMemberClass.setUpdateBy(staffResponse.getStaffName());
        storeMemberClass.setUpdateDate(new Date());
        return dao.updateByPrimaryKeySelective(storeMemberClass1);
    }

    @Override
    public int updateState(StoreMemberClassRequest storeMemberClassRequest, String phone) throws SmsException {
        StoreMemberClass storeMemberClass = dao.selectByPrimaryKey(storeMemberClassRequest.getId());
        if(storeMemberClassRequest.getState()==1){
            StoreMemberList storeMemberList = new StoreMemberList();
            storeMemberList.setClassId(storeMemberClass.getId());
            List<StoreMemberList> select = ssmStoreMemberService.select(storeMemberList);
            if(select.size()==0){
                storeMemberClass.setState(storeMemberClassRequest.getState());
                int update = dao.updateByPrimaryKeySelective(storeMemberClass);
                return update;
            }else{
                throw new SmsException(ResponseCode.MSG,"修改失败，该等级信息有会员关联");
            }

        }else{
            storeMemberClass.setState(storeMemberClassRequest.getState());
            int update = dao.updateByPrimaryKeySelective(storeMemberClass);
        }

        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(StoreMemberClassRequest storeMemberClassRequest) throws SmsException {
        StoreMemberList storeMemberList = new StoreMemberList();
        storeMemberList.setClassId(storeMemberClassRequest.getId());
        List<StoreMemberList> select = ssmStoreMemberService.select(storeMemberList);
        if(select.size()==0){
            StoreMemberClass storeMemberClass = new StoreMemberClass();
            storeMemberClass.setId(storeMemberClassRequest.getId());
            return dao.deleteByPrimaryKey(storeMemberClass.getId());
        }else{
            throw new SmsException(ResponseCode.MSG,"修改失败，该等级信息有会员关联");
        }
    }

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


}

