package com.dhcc.finance.main.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.common.constant.SysConstants;
import com.dhcc.common.constant.SysStatusCode;
import com.dhcc.common.model.CurrencyMeta;
import com.dhcc.common.model.SysUserInfo;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.common.util.CacheDataUtil;
import com.dhcc.common.util.SnowflakeIdWorker;
import com.dhcc.finance.main.dao.CwBdAccountingMapper;
import com.dhcc.finance.main.domain.CwBdAccounting;
import com.dhcc.finance.main.service.CwBdAccountingService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/** 功能描述：人员设置相关功能
 * @author hezewei
 * @date 2019年6月13日 下午8:08:23
 * @修改日志：
 */
@Service
public class CwBdAccountingServiceImpl implements CwBdAccountingService{
    @Autowired
    CwBdAccountingMapper cwBdAccountingMapper;
    
    @Autowired
    private Tracer tracer;
    
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /** 功能描述：获取全部人员设置信息
     * @param pageNum
     * @param pageSize
     * @param navigatePages
     * @param pkPrepared
     * @param pkAudit
     * @param pkSign
     * @return
     * @author hezewei
     * @date 2019年6月13日 下午8:12:35
     * @修改日志：
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public RestfulResponse<PageInfo<CwBdAccounting>, CurrencyMeta> searchCwBdAccountingAllData(int pageNum, int pageSize, int navigatePages, String pkPrepared, String pkAudit, String pkSign, String pkAccounting)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<CwBdAccounting> list = cwBdAccountingMapper.selectCwBdAccountingByLike(pkPrepared,pkAudit,pkSign,pkAccounting);
        PageInfo<CwBdAccounting> pageInfo = new PageInfo<>(list, navigatePages);
        return new RestfulResponse<PageInfo<CwBdAccounting>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
    }

    /** 功能描述：新增人员设置信息
     * @param cwGlTransferdetail
     * @return
     * @author hezewei
     * @date 2019年6月14日 上午11:05:25
     * @修改日志：
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public RestfulResponse<Object, CurrencyMeta> addCwBdAccounting(CwBdAccounting cwBdAccounting)
    {
        SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
        if (sysUserInfo != null) {
            cwBdAccounting.setCreateUser(sysUserInfo.getUserId());
            cwBdAccounting.setUpdateUser(sysUserInfo.getUserId());
        }else {
             return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.USER_LOGOUT_ERROR.message(),
                        MetaDataUtil.getMeta(tracer,SysStatusCode.USER_LOGOUT_ERROR));
        }
        cwBdAccounting.setPkPrepared(cwBdAccounting.getPkPrepared());
        cwBdAccounting.setPkAudit(cwBdAccounting.getPkAudit());
        cwBdAccounting.setPkSign(cwBdAccounting.getPkSign());
        cwBdAccounting.setEnablestate(cwBdAccounting.getEnablestate());
        cwBdAccounting.setRemark(cwBdAccounting.getRemark());
        cwBdAccounting.setPkAccounting(SnowflakeIdWorker.getInstance().nextIdString());
        cwBdAccounting.setCreateTime(new Date());
        cwBdAccounting.setUpdateTime(new Date());
        cwBdAccounting.setUseState("1");
        int res = cwBdAccountingMapper.insert(cwBdAccounting);
        if (res > 0) {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
        }else {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
        }
    }

    /** 功能描述：删除人员设置信息
     * @param pkAccounting
     * @return
     * @author hezewei
     * @date 2019年6月15日 上午11:01:39
     * @修改日志：
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestfulResponse<Object, CurrencyMeta> deleteCwBdAccounting(String pkAccounting)
    {
        CwBdAccounting cwBdAccounting = new CwBdAccounting();
        SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
        if (sysUserInfo != null) {
            cwBdAccounting.setUpdateUser(sysUserInfo.getUserId());
        }else {
                 return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.USER_LOGOUT_ERROR.message(),
                     MetaDataUtil.getMeta(tracer,SysStatusCode.USER_LOGOUT_ERROR));
              }
        cwBdAccounting.setUpdateTime(new Date());
        cwBdAccounting.setUseState("0");
        cwBdAccounting.setPkAccounting(pkAccounting);
        int code = cwBdAccountingMapper.deletePersonnelInformation(cwBdAccounting);
        if (code > 0) {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
        }else {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
        }
    }

    /** 功能描述：启用，禁用人员设置信息
     * @param enablestate
     * @return
     * @author hezewei
     * @date 2019年6月15日 下午3:21:50
     * @修改日志：
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestfulResponse<Object, CurrencyMeta> updateCwBdAccounting(String enablestate, String pkAccounting)
    {
        CwBdAccounting cwBdAccounting = new CwBdAccounting();
        SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
        if (sysUserInfo == null) {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.USER_LOGOUT_ERROR.message(),
                    MetaDataUtil.getMeta(tracer,SysStatusCode.USER_LOGOUT_ERROR));
        }
        if (enablestate.equals("0") || enablestate.equals("2"))
        {
            enablestate = "1";
        } else if (enablestate.equals("1"))
        {
            enablestate = "2";
        }
        cwBdAccounting.setEnablestate(enablestate);
        cwBdAccounting.setPkAccounting(pkAccounting);
        int code = cwBdAccountingMapper.updateByPrimaryKeySelective(cwBdAccounting);
        if (code > 0) {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
        }else {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
        }
    }

    /** 功能描述：修改人员设置信息
     * @param pkAccounting
     * @return
     * @author hezewei
     * @date 2019年6月17日 上午10:48:32
     * @修改日志：
     */
    @Override
    public RestfulResponse<Object, CurrencyMeta> updatePersonnelSettings(String pkPrepared, String pkAudit, String pkSign, String remark, String enablestate, String pkAccounting)
    {
        CwBdAccounting cwBdAccounting = new CwBdAccounting();
        SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
        if (sysUserInfo != null) {
            cwBdAccounting.setUpdateUser(sysUserInfo.getUserId());
        }else {
                 return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.USER_LOGOUT_ERROR.message(),
                     MetaDataUtil.getMeta(tracer,SysStatusCode.USER_LOGOUT_ERROR));
              }
        cwBdAccounting.setPkPrepared(pkPrepared);
        cwBdAccounting.setPkAudit(pkAudit);
        cwBdAccounting.setPkSign(pkSign);
        cwBdAccounting.setEnablestate(enablestate);
        cwBdAccounting.setRemark(remark);
        cwBdAccounting.setUpdateTime(new Date());
        cwBdAccounting.setPkAccounting(pkAccounting);
        int code = cwBdAccountingMapper.updateByPrimaryKeySelective(cwBdAccounting);
        if (code > 0) {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
        }else {
            return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
        }
    }
}
