package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.Activity;
import com.lightinit.hsdataplatform.entity.ActivityExample;
import com.lightinit.hsdataplatform.entity.ActivityWithBLOBs;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.mapper.ActivityMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.ActivitySearchModel;
import com.lightinit.hsdataplatform.service.admin.IActivityMgrService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by WangYao on 2018/5/20.
 */
@Service
public class ActivityMgrServiceImpl implements IActivityMgrService {

    @Autowired
    private ActivityMapper activityMapper;

    public ActivityWithBLOBs queryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return activityMapper.selectByPrimaryKey(id);
    }

    public List<Activity> queryList(ActivitySearchModel inputModel, BaseExample.Page page) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ActivityExample example=new ActivityExample();
        if (inputModel != null){
            ActivityExample.Criteria criteria=example.createCriteria();
            if (StringUtils.isNotEmpty(inputModel.getActivityName())){
                criteria.andActivityNameLike("%" + inputModel.getActivityName() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getActivityType())){
                criteria.andActivityTypeEqualTo(inputModel.getActivityType());
            }
            if (StringUtils.isNotEmpty(inputModel.getActivityState())){
                criteria.andActivityStateEqualTo(inputModel.getActivityState());
            }
        }
        example.setPage(page);
        return activityMapper.selectByExample(example);
    }

    public long queryCount(ActivitySearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ActivityExample example=new ActivityExample();
        if (inputModel != null){
            ActivityExample.Criteria criteria=example.createCriteria();
            if (StringUtils.isNotEmpty(inputModel.getActivityName())){
                criteria.andActivityNameLike("%" + inputModel.getActivityName() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getActivityType())){
                criteria.andActivityTypeEqualTo(inputModel.getActivityType());
            }
            if (StringUtils.isNotEmpty(inputModel.getActivityState())){
                criteria.andActivityStateEqualTo(inputModel.getActivityState());
            }
        }
        return activityMapper.countByExample(example);
    }

    public ResultState<Long> add(ActivityWithBLOBs entity) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        entity.setCreatedAt(new Date());
        long sqlResult = activityMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    public ResultState<Long> edit(ActivityWithBLOBs entity) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ActivityExample example=new ActivityExample();
        ActivityExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(entity.getId());
        long sqlResult = activityMapper.updateByExampleSelective(entity, example);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    public ResultState<Long> delete(long id) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ActivityExample example=new ActivityExample();
        ActivityExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        if (!(activityMapper.deleteByExample(example)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }
}
