package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicLeaveMessageState;
import com.lightinit.hsdataplatform.entity.LeaveMessage;
import com.lightinit.hsdataplatform.entity.LeaveMessageExample;
import com.lightinit.hsdataplatform.mapper.LeaveMessageMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.LeaveMessageDetailModel;
import com.lightinit.hsdataplatform.model.admin.LeaveMessageListModel;
import com.lightinit.hsdataplatform.model.admin.LeaveMessageSearchModel;
import com.lightinit.hsdataplatform.service.admin.ILeaveMessageService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @Author: sunxiang
 * @Date: 2018/7/11 14:43
 * @Description:
 **/
@Service
public class LeaveMessageServiceImpl implements ILeaveMessageService {

    @Autowired
    private LeaveMessageMapper leaveMessageMapper;
    @Override
    public List<LeaveMessageListModel> getList(LeaveMessageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return leaveMessageMapper.getList(inputModel);
    }

    @Override
    public long getListCount(LeaveMessageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return leaveMessageMapper.getListCount(inputModel);
    }

    @Override
    public LeaveMessageDetailModel getDetail(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return leaveMessageMapper.getDetail(id);
    }

    @Transactional
    @Override
    public ResultState edit(Long id, String feedbackContent, Long userId) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            LeaveMessage leaveMessage = new LeaveMessage();
            leaveMessage.setId(id);
            leaveMessage.setFeedbackAt(new Date());
            leaveMessage.setFeedbackContent(feedbackContent);
            leaveMessage.setFeedbackResult(DicLeaveMessageState.DONE);
            leaveMessage.setFeedbackBy(userId);
            leaveMessage.setMsgState(DicLeaveMessageState.DONE);
            leaveMessageMapper.updateByPrimaryKeySelective(leaveMessage);
            resultState.setMsg("反馈成功");
            return resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("反馈失败");
        return resultState;
    }

    @Transactional
    @Override
    public ResultState delete(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            leaveMessageMapper.deleteByPrimaryKey(id);
            resultState.setMsg("反馈成功");
            return resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("反馈失败");
        return resultState;
    }
}
