package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmSysCompanyDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.SysCompanyRequest;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.SsmSysCompanyService;
import com.ssm.util.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.ssm.manage.service.impl.SsmStaffWorksServiceImpl.getKey;

@Service
public class SsmSysCompanyServiceImpl  extends BaseServiceImpl<SysCompany> implements SsmSysCompanyService {
    @Autowired
    private SsmSysCompanyDao dao;

    @Autowired
    public RedisClient<UserResponse> redisClient;

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

    @Override
    public PageInfo<SysCompany> selectPage(SysCompanyRequest sysCompanyRequest) {
        return dao.selectPage(sysCompanyRequest);
    }

    @Override
    public int saveSysCompany(SysCompany sysCompany,String userNum) throws SmsException {
        //从redis获取当前操作人信息 将添加用户的创建人和创建时间进行赋值
        if (redisClient.hasKey(getKey(userNum))){
            UserResponse userResponse = redisClient.get(getKey(userNum));
            sysCompany.setFunderName(userResponse.getUserName());
            sysCompany.setFunderDate(new Date());
        }
        //设置主键
        int i = dao.saveNotNull(sysCompany);
        return i;
    }

    @Override
    public int updateSysCompany(SysCompany sysCompany, String userNum) throws SmsException {
        //从redis获取当前操作人信息 将修改用户的修改人和修改时间进行赋值
        if (redisClient.hasKey(getKey(userNum))){
            UserResponse userResponse = redisClient.get(getKey(userNum));
            sysCompany.setUpdatedName(userResponse.getUserName());
            sysCompany.setUpdatedDate(new Date());
        }
        int i = dao.updateByPrimaryKeySelective(sysCompany);
        return i;
    }
}
