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.AfAdviseTypeMapper;
import cn.com.countrygarden.bi.main.dto.advise.param.AdviseTypeParam;
import cn.com.countrygarden.bi.main.dto.advise.AfAdviseTypeVO;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseType;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

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

/**
 * 建言类型-业务逻辑层实现
 *
 * @author 木木子
 * @email 944528607@qq.com
 * @url
 * @date 2019-04-15
 */
@Service
public class AfAdviseTypeServiceImpl implements IAfAdviseTypeService {
	@Autowired
	private AfAdviseTypeMapper afAdviseTypeMapper;

    @Override
    public Result queryList(AdviseTypeParam adviseTypeParam, AccessToken accessToken) {

		//如果分支类型为空则默认为advise_juzhele（居者乐）
		if(StringUtils.isBlank(adviseTypeParam.getBranchTypeCode())){
			adviseTypeParam.setBranchTypeCode("advise_juzhele");
		}
		List<AfAdviseTypeVO> afAdviseTypeVOList = afAdviseTypeMapper.queryAllList(adviseTypeParam);
		return Response.response(afAdviseTypeVOList);
	}
	@Override
	public Result querySelectList(AdviseTypeParam adviseTypeParam, AccessToken accessToken) {
		//如果分支类型为空则默认为advise_juzhele（居者乐）
		if(StringUtils.isBlank(adviseTypeParam.getBranchTypeCode())){
			adviseTypeParam.setBranchTypeCode("advise_juzhele");
		}
		List<AfAdviseTypeVO>	afAdviseTypeVOList = afAdviseTypeMapper.queryList(adviseTypeParam);
		return Response.response(afAdviseTypeVOList);
	}

	@Override
	public Result queryById(Integer id) {
		return Response.response(afAdviseTypeMapper.selectByPrimaryKey(id));
	}

	@Override
	public Result addOrEdit(AfAdviseType afAdviseType, AccessToken accessToken) {
		if(null==afAdviseType.getEnable()){
			afAdviseType.setEnable(1);
		}

		if (afAdviseType.getId() != null && afAdviseType.getId() > 0) {
			afAdviseType.setUpdateBy(accessToken.getUserCode());
			afAdviseType.setUpdateTime(new Date());
			afAdviseTypeMapper.updateByPrimaryKeySelective(afAdviseType);
		} else {
			afAdviseType.setCreateBy(accessToken.getUserCode());
			afAdviseType.setCreateTime(new Date());
			afAdviseTypeMapper.insertSelective(afAdviseType);
		}

		return Response.response();
	}

	@Override
	public Result del(String ids) {
		String[] idsArray = ids.split(",");
		Condition condition = new Condition(AfAdviseType.class);
		condition.createCriteria().andIn("id", Arrays.asList(idsArray));
		AfAdviseType afAdviseType = new AfAdviseType();
		afAdviseType.setEnable(0);
		int count = afAdviseTypeMapper.updateByConditionSelective(afAdviseType, condition);
		return Response.response(count);
	}


}