package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmSysRoleUserDao;
import com.ssm.manage.dao.SsmSysStoreRoleDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.SysRoleStoreRequest;
import com.ssm.manage.query.SysStoreRoleRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.util.RedisClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class SsmSysStoreRoleServiceImpl  extends BaseServiceImpl<SysStoreRole> implements SsmSysStoreRoleService {
    @Autowired
    private SsmSysStoreRoleDao dao;

    @Autowired
    private SsmSysRoleStoreService ssmSysRoleStoreService;
    @Autowired
    private SsmSysMuenRoleService ssmSysMuenRoleService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

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

    @Autowired
    private RedisClient<StaffResponse> redisClient;
    private static String staffKey = "staffKey";


    @Override
    public PageInfo<SysStoreRole> selectPage(SysStoreRoleRequest sysStoreRole) {
        return dao.selectPage(sysStoreRole);
    }

    @Override
    public int saveSysStoreRole(SysStoreRoleRequest sysStoreRoles, String userNum) throws SmsException {
        SysStoreRole sysStoreRole = new SysStoreRole();
        BeanUtils.copyProperties(sysStoreRoles,sysStoreRole);
        //判断重复
        SysStoreRole sysStoreRole1 = new SysStoreRole();
        sysStoreRole1.setStoreId(sysStoreRole.getStoreId());
        sysStoreRole1.setRoleNum(sysStoreRole.getRoleNum());
        List<SysStoreRole> select = dao.select(sysStoreRole1);
        if(select.size()>0){
            throw new SmsException(ResponseCode.MSG,"当前组织门店角色编码已经存在");
        }

        sysStoreRole1.setRoleNum(null);
        sysStoreRole1.setRoleName(sysStoreRole.getRoleName());
        List<SysStoreRole> selects = dao.select(sysStoreRole1);
        if(selects.size()>0){
            throw new SmsException(ResponseCode.MSG,"当前组织门店角色名称已经存在");
        }

        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(userNum) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        sysStoreRole.setFounder(staffResponse.getStaffName());
        sysStoreRole.setFounderDatetime(new Date());
        sysStoreRole.setState(1);
        return dao.insert(sysStoreRole);
    }

    @Override
    public int updateSysStoreRole(SysStoreRoleRequest sysStoreRoles, String userNum) throws SmsException {

        SysStoreRole sysStoreRole = new SysStoreRole();
        BeanUtils.copyProperties(sysStoreRoles,sysStoreRole);
        SysStoreRole sysStoreRole2 = dao.selectByPrimaryKey(sysStoreRole.getId());
        //判断重复
        SysStoreRole sysStoreRole1 = new SysStoreRole();
        sysStoreRole1.setStoreId(sysStoreRole2.getStoreId());
        sysStoreRole1.setRoleNum(sysStoreRole.getRoleNum());
        List<SysStoreRole> select = dao.select(sysStoreRole1);
        if(select.size()>0){
            if(!select.get(0).getId().equals(sysStoreRole.getId())){
                throw new SmsException(ResponseCode.MSG,"当前组织门店角色编码已经存在");
            }

        }

        sysStoreRole1.setRoleNum(null);
        sysStoreRole1.setRoleName(sysStoreRole.getRoleName());
        List<SysStoreRole> selects = dao.select(sysStoreRole1);
        if(selects.size()>0){
            if(!select.get(0).getId().equals(sysStoreRole.getId())){
                throw new SmsException(ResponseCode.MSG,"当前组织门店角色名称已经存在");
            }
        }
        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(userNum) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        sysStoreRole.setModifyBy(staffResponse.getStaffName());
        sysStoreRole.setModifyDatetime(new Date());
        return dao.updateByPrimaryKeySelective(sysStoreRole);
    }

    @Override
    public int updateSysRoleState(SysStoreRoleRequest sysStoreRole) {
        SysStoreRole sysStoreRole2 = dao.selectByPrimaryKey(sysStoreRole.getId());
        sysStoreRole2.setState(sysStoreRole.getState());
        return dao.updateByPrimaryKeySelective(sysStoreRole2);
    }

    @Override
    public int addRoleStore(SysStoreRoleRequest sysStoreRole) throws SmsException {
        SysRoleStore sysRoleStore = new SysRoleStore();
        sysRoleStore.setRoleId(sysStoreRole.getId());
        int delete = ssmSysRoleStoreService.delete(sysRoleStore);
        SysStoreRole sysStoreRole1 = dao.selectByPrimaryKey(sysStoreRole.getId());
        if(sysStoreRole.getSysRoleStoreRequests().size()>0){
            List<SysRoleStoreRequest> sysRoleStoreRequests = sysStoreRole.getSysRoleStoreRequests();
            for (SysRoleStoreRequest sysRoleStoreRequest : sysRoleStoreRequests) {
                SysRoleStore sysRoleStore1 = new SysRoleStore();
                BeanUtils.copyProperties(sysRoleStoreRequest,sysRoleStore1);
                sysRoleStore1.setRoleId(sysStoreRole1.getId());
                sysRoleStore1.setRoleNum(sysStoreRole1.getRoleNum());
                sysRoleStore1.setRoleName(sysStoreRole1.getRoleName());
                sysRoleStore1.setStoreId(sysRoleStoreRequest.getId());
                sysRoleStore1.setId(null);
                int save = ssmSysRoleStoreService.save(sysRoleStore1);
                if(save== 0){
                    throw new SmsException(ResponseCode.MSG,"添加关联角色门店失败");
                }
            }
        }
        return 1;
    }

    @Override
    public int addMuenRole(SysStoreRoleRequest sysStoreRole) throws SmsException {
        SysMuenRole sysRoleStore = new SysMuenRole();
        sysRoleStore.setRoleId(sysStoreRole.getId());
        int delete = ssmSysMuenRoleService.delete(sysRoleStore);
        int save = ssmSysMuenRoleService.saveAll(sysStoreRole.getSysMuenRoles());
//        SysStoreRole sysStoreRole1 = dao.selectByPrimaryKey(sysStoreRole.getId());
//        if(sysStoreRole.getSysMuenRoles().size()>0){
//            List<SysMuenRole> sysRoleStoreRequests = sysStoreRole.getSysMuenRoles();
//            for (SysMuenRole sysRoleStoreRequest : sysRoleStoreRequests) {
//                sysRoleStoreRequest.setRoleId(sysStoreRole1.getId());
//                int save = ssmSysMuenRoleService.save(sysRoleStoreRequest);
//                if(save== 0){
//                    throw new SmsException(ResponseCode.MSG,"添加关联角色菜单失败");
//                }
//            }
//        }
        return 1;
    }

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

    public String getKey(Long storeId, String key){
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        key = baseStore.getStoreNum() + key;
        return key;
    }
}
