package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.KeyWord;
import com.lightinit.hsdataplatform.mapper.KeyWordMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.HotWordListModel;
import com.lightinit.hsdataplatform.model.admin.HotWordModel;
import com.lightinit.hsdataplatform.service.admin.IHotWordService;
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 HotWordServiceImpl implements IHotWordService {
    @Autowired
    private KeyWordMapper keyWordMapper;

    @Override
    public List<HotWordListModel> queryList() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return keyWordMapper.queryList();
    }

    @Override
    public ResultState add(HotWordModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<>();
        KeyWord model = new KeyWord();
        BeanUtils.copyProperties(inputModel,model);
        model.setCreatedAt(new Date());
        model.setCreatedBy(inputModel.getCreatedBy());
        int sqlresult = keyWordMapper.insert(model);
        if(!(sqlresult>0)) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("添加失败");
        }
        resultState.setData(model.getId());
        return resultState;
    }

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

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

    @Override
    public ResultState doMove(HotWordModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<>();
        KeyWord model = new KeyWord();
        BeanUtils.copyProperties(inputModel,model);
        int sqlresult= keyWordMapper.updateByPrimaryKey(model);
        if(!(sqlresult>0)) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("移动失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }
}
