package cn.com.countrygarden.bi.main.service.advise.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.advise.AfAdviseConfigMapper;
import cn.com.countrygarden.bi.main.dto.advise.AfAdviseConfigDTO;
import cn.com.countrygarden.bi.main.dto.advise.param.AdviseConfigParam;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseConfig;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseConfigService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * 建言基础设置-业务逻辑层实现
 *
 * @author 木木子
 * @email 944528607@qq.com
 * @url
 * @date 2019-04-15
 */
@Service
@Transactional
public class AfAdviseConfigServiceImpl implements IAfAdviseConfigService {
	@Autowired
	private AfAdviseConfigMapper afAdviseConfigMapper;

	@Override
	public Result queryPageList(AfAdviseConfig afAdviseConfig, int page, int size) {
		Page<Object> pageList = PageHelper.startPage(page, size);
		Condition condition = new Condition(AfAdviseConfig.class);

		afAdviseConfigMapper.selectByCondition(condition);
		PageData data = new PageData(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);
	}



	@Override
	public Result addOrEdit(AfAdviseConfigDTO afAdviseConfigDTO, AccessToken accessToken) {
		 int count=0;
		AfAdviseConfig afAdviseConfig = new AfAdviseConfig();
		afAdviseConfig.setEnable(1);
		afAdviseConfig.setBranchTypeCode(afAdviseConfigDTO.getBranchTypeCode());
		if (StringUtils.isNoneBlank(afAdviseConfigDTO.getConfigKey())&&StringUtils.isNoneBlank(afAdviseConfigDTO.getBranchTypeCode())) {
			if("timeOut".equals(afAdviseConfigDTO.getConfigKey())){
				afAdviseConfig.setName("超时时间");
			}else if("cutOff".equals(afAdviseConfigDTO.getConfigKey())){
				afAdviseConfig.setName("周期截点");
			}else {
				throw new ServiceException("操作失败");
			}

			afAdviseConfig.setConfigValue(afAdviseConfigDTO.getConfigValue());
			afAdviseConfig.setConfigKey(afAdviseConfigDTO.getConfigKey());

		Condition condition=new Condition(AfAdviseConfig.class);
		condition.createCriteria().andEqualTo("configKey",afAdviseConfigDTO.getConfigKey())
				.andEqualTo("branchTypeCode",afAdviseConfigDTO.getBranchTypeCode());
		 count=afAdviseConfigMapper.selectCountByCondition(condition);
		//configKey+branchTypeCode值要求唯一
		if(count>0) {
			afAdviseConfig.setUpdateBy(accessToken.getUserCode());
			afAdviseConfig.setUpdateTime(new Date());
			count=afAdviseConfigMapper.updateByConditionSelective(afAdviseConfig,condition);
		}else {
			afAdviseConfig.setCreateBy(accessToken.getUserCode());
			afAdviseConfig.setCreateTime(new Date());
			count=afAdviseConfigMapper.insertSelective(afAdviseConfig);
		}
		}
		return Response.response(count);
	}

	@Override
	public Result del(Integer id) {
		int count = afAdviseConfigMapper.deleteByPrimaryKey(id);
		return Response.response(count);
	}



	@Override
	public Result queryByOne(AdviseConfigParam adviseConfigParam) {
		AfAdviseConfig afAdviseConfig=null;
		if(StringUtils.isNoneBlank(adviseConfigParam.getConfigKey())&&StringUtils.isNoneBlank(adviseConfigParam.getBranchTypeCode())) {
			afAdviseConfig=new AfAdviseConfig();
			afAdviseConfig.setConfigKey(adviseConfigParam.getConfigKey());
			afAdviseConfig.setBranchTypeCode(adviseConfigParam.getBranchTypeCode());
			afAdviseConfig.setEnable(1);
			afAdviseConfig=afAdviseConfigMapper.selectOne(afAdviseConfig);
		}
		return Response.response(afAdviseConfig);
	}



}