package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.Resource;
import com.lightinit.hsdataplatform.mapper.ResourceMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.AggregationModel;
import com.lightinit.hsdataplatform.model.admin.ClassificationAndAggregationListModel;
import com.lightinit.hsdataplatform.model.admin.ClassificationAndAggregationSearchModel;
import com.lightinit.hsdataplatform.service.admin.IClassificationAndAggregationService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ClassificationAndAggregationServiceImpl implements IClassificationAndAggregationService {
   @Autowired
   private ResourceMapper resourceMapper;
    @Override
    public List<ClassificationAndAggregationListModel> queryList(ClassificationAndAggregationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceMapper.queryClassificationAndAggregationList(inputModel);
    }

    @Override
    public long queryCount(ClassificationAndAggregationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceMapper.queryClassificationAndAggregationCount(inputModel);
    }

    @Override
    public Resource aggregation(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState toAggregation(AggregationModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Resource  resource = new Resource();
        BeanUtils.copyProperties(inputModel, resource);
        int sqlResult = 0;
        sqlResult = resourceMapper.updateByPrimaryKeySelective(resource);
        if (!(sqlResult > 0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("归类失败");
        }
        resultState.setData(resource.getId());
        return resultState;
    }
}
