package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.common.ObjectMappingUtils;
import com.lightinit.hsdataplatform.dictionary.DicResourceAuditResult;
import com.lightinit.hsdataplatform.dictionary.DicResourceState;
import com.lightinit.hsdataplatform.dictionary.DicResourceType;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.PublishInfoMapper;
import com.lightinit.hsdataplatform.mapper.ResourceDataMapper;
import com.lightinit.hsdataplatform.mapper.ResourceMapper;
import com.lightinit.hsdataplatform.mapper.SectorMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IResourceDataRegisterService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by WangYao on 2018/5/23.
 */
@Service
public class ResourceDataRegisterServiceImpl implements IResourceDataRegisterService {

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private ResourceDataMapper resourceDataMapper;
    @Autowired
    private  SectorMapper  sectorMapper;
    @Autowired
    private PublishInfoMapper publishInfoMapper;

    //判断是否是admin,是：将id置为null;
    private ResourceDataRegisterSearchModel getRolecodeByUserId(ResourceDataRegisterSearchModel inputModel){
        List<String> RolecodeList=publishInfoMapper.getRolecodeByUserId(inputModel.getUserId());
        for (String s : RolecodeList) {
            if(s.equals("admin")){
                inputModel.setUserId(null);
                break;
            }
        }
        return inputModel;
    }

    public ResourceDataRegisterDataModel queryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResourceDataRegisterDataModel outputModel = new ResourceDataRegisterDataModel();
        Resource entity1 = resourceMapper.selectByPrimaryKey(id);
        ResourceDataWithBLOBs entity2 = resourceDataMapper.selectByPrimaryKey(id);
        Sector sector = sectorMapper.selectByPrimaryKey(entity2.getBelongTo());
        if(sector!=null){
            outputModel.setBelongToString(sector.getSectorName());
        }
        BeanUtils.copyProperties(entity2, outputModel);
        BeanUtils.copyProperties(entity1, outputModel);
        return outputModel;
    }

    public List<ResourceDataRegisterListModel> queryList(ResourceDataRegisterSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel = getRolecodeByUserId(inputModel);
        inputModel.setResourceType(DicResourceType.DATA);
        return resourceDataMapper.selectModelList(inputModel);
    }

    public long queryCount(ResourceDataRegisterSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel = getRolecodeByUserId(inputModel);
        inputModel.setResourceType(DicResourceType.DATA);
        return resourceDataMapper.selectModelListCount(inputModel);
    }

    @Transactional
    public ResultState<Long> add(ResourceDataRegisterDataModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Date createdAt = new Date();

        Resource entity1 = new Resource();
        BeanUtils.copyProperties(inputModel, entity1);
        entity1.setResourceType(DicResourceType.DATA);
        entity1.setResourceState(StringUtils.isNotEmpty(inputModel.getResourceState()) && inputModel.getResourceState().equals(DicResourceState.DRAFT) ? DicResourceState.DRAFT : DicResourceState.PENDING);
        entity1.setRegisterDate(createdAt);
        entity1.setCreatedAt(createdAt);
        int sqlResult1 = resourceMapper.insert(entity1);

        ResourceDataWithBLOBs entity2 = new ResourceDataWithBLOBs();
        BeanUtils.copyProperties(inputModel, entity2);
        entity2.setId(entity1.getId());
        entity2.setCreatedAt(createdAt);
        int sqlResult2 = resourceDataMapper.insert(entity2);

        if (!(sqlResult1 > 0 && sqlResult2 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity1.getId());
        return resultState;
    }

    @Transactional
    public ResultState<Long> edit(ResourceDataRegisterDataModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();

        Resource resource = resourceMapper.selectByPrimaryKey(inputModel.getId());
        if (resource == null){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
            return resultState;
        }

        Resource entity1 = new Resource();
        BeanUtils.copyProperties(inputModel, entity1);
        if (!(DicResourceState.DRAFT.equals(resource.getResourceState()) && DicResourceState.PENDING.equals(inputModel.getResourceState()))){
            entity1.setResourceState(null);
        }
        ResourceExample example1 = new ResourceExample();
        ResourceExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(inputModel.getId());
        int sqlResult1 = resourceMapper.updateByExampleSelective(entity1, example1);

        ResourceDataWithBLOBs entity2 = new ResourceDataWithBLOBs();
        BeanUtils.copyProperties(inputModel, entity2);
        ResourceDataExample example2 = new ResourceDataExample();
        ResourceDataExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andIdEqualTo(inputModel.getId());
        int sqlResult2 = resourceDataMapper.updateByExampleSelective(entity2, example2);

        if (!(sqlResult1 > 0 && sqlResult2 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Transactional
    public ResultState<Long> delete(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();

        ResourceDataExample example1 = new ResourceDataExample();
        ResourceDataExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(id);
        int sqlResult1 = resourceDataMapper.deleteByExample(example1);

        ResourceExample example2 = new ResourceExample();
        ResourceExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andIdEqualTo(id);
        int sqlResult2 = resourceMapper.deleteByExample(example2);

        if (!(sqlResult1 > 0 && sqlResult2 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }



    @Transactional
    public ResultState<Long> audit(ResourceDataRegisterAuditModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();

        Resource resource = resourceMapper.selectByPrimaryKey(inputModel.getId());
        if (resource == null){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
            return resultState;
        }

        Resource entity1 = new Resource();
        BeanUtils.copyProperties(inputModel, entity1);
        entity1.setResourceState(inputModel.getAuditResult());
        ResourceExample example1 = new ResourceExample();
        ResourceExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(inputModel.getId());
        int sqlResult1 = resourceMapper.updateByExampleSelective(entity1, example1);

        if (!(sqlResult1 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }
}
