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.SsmBasePostDao;
import com.ssm.manage.dao.SsmBaseStaffDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.BasePostRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.query.StoreRoomRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmBasePostService;
import com.ssm.manage.service.SsmBaseStaffService;
import com.ssm.manage.service.SsmBaseStoreService;
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 SsmBasePostServiceImpl  extends BaseServiceImpl<BasePost> implements SsmBasePostService {
    @Autowired
    private SsmBasePostDao dao;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;
    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

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

    private static String staffKey = "staffKey";

    @Autowired
    private RedisClient<StaffResponse> redisClient;

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

        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(storeRoomRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null ==pagesorganization.getParentId()){
                storeRoomRequest.setStoreId(pagesorganization.getId());
            }
        }

        criteria.andEqualTo("storeId",storeRoomRequest.getStoreId());
        if (null != storeRoomRequest.getPostNum()){
            criteria.andLike("postNum","%" + storeRoomRequest.getPostNum() + "%");
        }
        if (null != storeRoomRequest.getPostName()){
            criteria.andLike("postName","%" + storeRoomRequest.getPostName() + "%");
        }
        if (null != storeRoomRequest.getState()){
            criteria.andEqualTo("state",storeRoomRequest.getState());
        }
        storeRoomRequest.setSort("state");
        PageHelper.startPage(storeRoomRequest.getPageNum(),storeRoomRequest.getPageSize(),storeRoomRequest.getOrderAndSort());
        List<BasePost> list = selectByExample(example);
        PageInfo<BasePost> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(BasePostRequest storeRoomRequest, String phone) throws SmsException {
        BasePost storeRoom = new BasePost();
        storeRoom.setStoreId(storeRoomRequest.getStoreId());
        storeRoom.setPostNum(storeRoomRequest.getPostNum());
        BasePost select = dao.selectOne(storeRoom);
        if(null != select){
            throw new SmsException(ResponseCode.MSG,"当前职务编码已经存在");
        }
        storeRoom.setPostNum(null);
        storeRoom.setPostName(storeRoomRequest.getPostName());
        BasePost selects = dao.selectOne(storeRoom);
        if(null != selects){
            throw new SmsException(ResponseCode.MSG,"当前职务名称已经存在");
        }

        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);
        }
        BaseStore baseStore = new BaseStore();
        baseStore.setId(storeRoomRequest.getStoreId());
        BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(baseStore.getId());
        storeRoom.setStoreName(baseStore1.getStoreName());
        storeRoom.setStoreNum(baseStore1.getStoreNum());
        storeRoom.setPostNum(storeRoomRequest.getPostNum());
        storeRoom.setInputBy(staffResponse.getStaffName());
        storeRoom.setInputDate(new Date());
        storeRoom.setInputId(staffResponse.getId());

        storeRoom.setState(0);
        int insert = dao.insert(storeRoom);
        if(insert==0){
            throw new SmsException(ResponseCode.MSG,"新增失败");
        }
        return insert;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int update(BasePostRequest storeRoomRequest, String phone) throws SmsException {
        BasePost storeRoom = new BasePost();
        BasePost basePost = dao.selectByPrimaryKey(storeRoomRequest.getId());
        BeanUtils.copyProperties(basePost,storeRoom);
        if(!storeRoomRequest.getPostNum().equals(basePost.getPostNum()) || !storeRoomRequest.getPostName().equals(basePost.getPostName()) ){
            BaseStaff baseStaff = new BaseStaff();
            baseStaff.setPostId(basePost.getId());

            List<BaseStaff> select1 = ssmBaseStaffService.select(baseStaff);
            if(select1.size()>0){
                throw new SmsException(ResponseCode.MSG,"当前职务已经存在员工关联，不允许修改名称和编码");
            }

            storeRoom.setStoreId(storeRoomRequest.getStoreId());
            storeRoom.setPostNum(storeRoomRequest.getPostNum());
            BasePost select = dao.selectOne(storeRoom);
            if(null != select){
                if(!storeRoomRequest.getPostNum().equals(select.getPostNum())){
                    throw new SmsException(ResponseCode.MSG,"当前职务编码已经存在");
                }
            }
            storeRoom.setPostNum(null);
            storeRoom.setPostName(storeRoomRequest.getPostName());
            BasePost selects = dao.selectOne(storeRoom);
            if(null != selects){
                if(!storeRoomRequest.getPostName().equals(selects.getPostName())){
                    throw new SmsException(ResponseCode.MSG,"当前职务名称已经存在");
                }
            }
        }

        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);
        }
        storeRoom.setUpdateBy(staffResponse.getStaffName());
        storeRoom.setUpdateDate(new Date());
        storeRoom.setUpdateId(staffResponse.getId());

        int insert = dao.updateByPrimaryKeySelective(storeRoom);
        if(insert==0){
            throw new SmsException(ResponseCode.MSG,"修改失败");
        }
        return insert;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int detele(BasePostRequest storeRoomRequest) throws SmsException {
        BaseStaff baseStaff = new BaseStaff();
        baseStaff.setPostId(storeRoomRequest.getId());

        List<BaseStaff> select1 = ssmBaseStaffService.select(baseStaff);
        if(select1.size()>0){
            throw new SmsException(ResponseCode.MSG,"当前职务已经存在员工关联，不允许删除");
        }
        return dao.deleteByPrimaryKey(storeRoomRequest.getId());
    }


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

}
