package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicNewsState;
import com.lightinit.hsdataplatform.entity.News;
import com.lightinit.hsdataplatform.entity.NewsContent;
import com.lightinit.hsdataplatform.entity.NewsWithBLOBs;
import com.lightinit.hsdataplatform.mapper.NewsContentMapper;
import com.lightinit.hsdataplatform.mapper.NewsMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.ConsultSearchModel;
import com.lightinit.hsdataplatform.model.admin.NewModel;
import com.lightinit.hsdataplatform.service.admin.IConsultService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
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 javax.xml.transform.TransformerConfigurationException;
import java.util.Date;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/7/5 17:12
 * @Description:
 **/
@Service
public class ConsultServiceImpl implements IConsultService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper newsContentMapper;
    @Transactional
    @Override
    public ResultState add(NewModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState<>();
        try {
            NewsWithBLOBs newsWithBLOBs = new NewsWithBLOBs();
            BeanUtils.copyProperties(inputModel,newsWithBLOBs);
            newsWithBLOBs.setCreatedAt(new Date());
            newsWithBLOBs.setPublishAt(new Date());
            newsWithBLOBs.setNewsState(DicNewsState.PUBLISHED);
            newsWithBLOBs.setPublishBy(inputModel.getUserId());
            newsMapper.insert(newsWithBLOBs);
            NewsContent newsContent = new NewsContent();
            BeanUtils.copyProperties(inputModel,newsContent);
            newsContent.setId(newsWithBLOBs.getId());
            newsContentMapper.insertNewContent(newsContent);
            resultState.setMsg("保存成功");
            resultState.setData(inputModel.getNewsContent());
            return resultState;
        } catch (BeansException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("数据插入失败");
        return resultState;
    }

    @Override
    public List<NewModel> getList(ConsultSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return newsMapper.getNewsList(inputModel);
    }


    @Override
    public int queryCount(ConsultSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return newsMapper.queryNewsCount(inputModel);
    }
    @Transactional
    @Override
    public ResultState update(NewModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            NewsContent newsContent = new NewsContent();
            BeanUtils.copyProperties(inputModel,newsContent);
            newsContentMapper.updateByPrimaryKeySelective(newsContent);
            NewsWithBLOBs newsWithBLOBs = new NewsWithBLOBs();
            BeanUtils.copyProperties(inputModel,newsWithBLOBs);
            newsMapper.updateByPrimaryKeySelective(newsWithBLOBs);
            resultState.setMsg("操作成功");
            return resultState;
        } catch (BeansException 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 {
            newsContentMapper.deleteByPrimaryKey(id);
            newsMapper.deleteByPrimaryKey(id);
            resultState.setMsg("删除成功");
            return  resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("删除失败");
        return resultState;
    }
}
