package com.lightinit.hsdataplatformresdir.impl.admin;

import com.lightinit.hsdataplatformresdir.dictionary.DicDataPublishState;
import com.lightinit.hsdataplatformresdir.dictionary.DicResourceAuditResult;
import com.lightinit.hsdataplatformresdir.entity.PublishInfoWithBLOBs;
import com.lightinit.hsdataplatformresdir.entity.Sector;
import com.lightinit.hsdataplatformresdir.mapper.PublishInfoMapper;
import com.lightinit.hsdataplatformresdir.mapper.SectorMapper;
import com.lightinit.hsdataplatformresdir.model.ResultState;
import com.lightinit.hsdataplatformresdir.model.ResultStateCode;
import com.lightinit.hsdataplatformresdir.model.admin.DataPublishDetailModel;
import com.lightinit.hsdataplatformresdir.model.admin.DataPublishListModel;
import com.lightinit.hsdataplatformresdir.model.admin.DataPublishSearchModel;
import com.lightinit.hsdataplatformresdir.service.admin.IDataAuditService;
import com.lightinit.hsdataplatformresdir.service.admin.IDataPublishMgrService;
import com.lightinit.hsdataplatformresdir.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/6/5 17:30
 * @Description:
 **/
@Service
public class DataAuditServiceImpl implements IDataAuditService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;
    @Autowired
    private IDataPublishMgrService dataPublishMgrService;
    @Autowired
    private SectorMapper sectorMapper;

        //判断是否是admin,是：将id置为null;
        private DataPublishSearchModel getRolecodeByUserId(DataPublishSearchModel inputModel){
            List<String> RolecodeList=publishInfoMapper.getRolecodeByUserId(inputModel.getUserId());
            for (String s : RolecodeList) {
                if(s.equals("admin")){
                    inputModel.setUserId(null);
                    break;
                }
            }
            return inputModel;
        }
    @Override
    public List<DataPublishListModel> queryList(DataPublishSearchModel inputModel) {//返回数据有误，待修改
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setDicDataPublishState(DicDataPublishState.PENDING);
        inputModel= getRolecodeByUserId(inputModel);
        return publishInfoMapper.queryList(inputModel);
        //return publishInfoMapper.queryDataPublishNotAuditList(inputModel);
    }

    @Override
    public long queryCount(DataPublishSearchModel inputModel) {//返回数据有误，待修改
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setDicDataPublishState(DicDataPublishState.PENDING);
        inputModel= getRolecodeByUserId(inputModel);
        return publishInfoMapper.queryListCount(inputModel);
       // return publishInfoMapper.queryDataPublishNotAuditCount(inputModel);
    }

    @Override
    public DataPublishDetailModel queryOne(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        DataPublishDetailModel dataPublishDetailModel = dataPublishMgrService.queryPublishInfoOne(id);
        Sector sector = sectorMapper.selectByPrimaryKey(dataPublishDetailModel.getBelongTo());
        dataPublishDetailModel.setBelongToName(sector.getSectorName());
        return dataPublishDetailModel;
    }

    @Transactional
    @Override
    public ResultState<Long> audit(Long id, String auditComment, int param, Long userId) {
        ResultState<Long> resultState = new ResultState<>();
        try {
            DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
            PublishInfoWithBLOBs publishInfoWithBLOBs = new PublishInfoWithBLOBs();
            publishInfoWithBLOBs.setAuditedBy(userId);
            publishInfoWithBLOBs.setId(id);
            if(param==1){
                publishInfoWithBLOBs.setPublishState(DicDataPublishState.PUBLISHED);
                publishInfoWithBLOBs.setAuditResult(DicResourceAuditResult.APPROVED);
            }else{
                publishInfoWithBLOBs.setPublishState(DicDataPublishState.REJECTED);
                publishInfoWithBLOBs.setAuditResult(DicResourceAuditResult.REJECTED);
            }
            publishInfoWithBLOBs.setAuditedAt(new Date());
            publishInfoMapper.updateByPrimaryKeySelective(publishInfoWithBLOBs);
            return resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("数据插入失败");
        return resultState;
    }
}
