package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.ResourceSort;
import com.lightinit.hsdataplatform.mapper.ResourceSortMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.DataRecommenAddModel;
import com.lightinit.hsdataplatform.model.admin.DataRecommendationListModel;
import com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel;
import com.lightinit.hsdataplatform.service.admin.IDataRecommendationService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class DataRecommendationServiceImpl implements IDataRecommendationService {
    @Autowired
    private ResourceSortMapper resourceSortMapper;

    @Override
    public List<DataRecommendationListModel> queryList(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryList(inputModel);
    }

    @Override
    public long queryCount(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryCount(inputModel);
    }

    @Override
    public long queryResourceCount(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryResourceCount(inputModel);
    }

    @Override
    public List<DataRecommendationListModel> queryResourceList(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryResourceList(inputModel);
    }

    @Override
    public ResultState doAdd(DataRecommenAddModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        ResourceSort resourceSort = new ResourceSort();
        resourceSort.setResourceId(inputModel.getResourceId());
        resourceSort.setCreatedAt(new Date());
        resourceSort.setSortCategory(inputModel.getSortCategory());
        resourceSort.setSortValue(inputModel.getSortValue());
       int sqlResult1 =  resourceSortMapper.insert(resourceSort);
        if (!(sqlResult1 > 0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(resourceSort.getId());
        return resultState;
    }

    @Override
    public ResourceSort selectById(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState doMove(DataRecommenAddModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
            ResultState<Long> resultState = new ResultState<Long>();
        ResourceSort resourceSort = new ResourceSort();
        BeanUtils.copyProperties(inputModel,resourceSort);
        int sqlResult1=0;
        if(inputModel.getId()!=null) {
             sqlResult1 =  resourceSortMapper.updateByPrimaryKey(resourceSort);
        }else {
             sqlResult1 =  resourceSortMapper.insert(resourceSort);
        }

        if (!(sqlResult1 > 0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("移动顺序失败");
        }
        resultState.setData(resourceSort.getId());
        return resultState;
    }

    @Override
    public List<DataRecommendationListModel> queryDefaultList(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryDefaultList(inputModel);
    }

    @Override
    public long queryDefaultCount(DataRecommendationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceSortMapper.queryDefaultCount(inputModel);
    }

    @Override
    public ResultState delete(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        int sqlResult1 = resourceSortMapper.deleteByPrimaryKey(id);
        if (!(sqlResult1 > 0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("删除失败");
        }
        resultState.setData(id);
        return resultState;
    }
}
