package com.hys.app.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.util.PageConvert;
import com.hys.app.framework.util.StringUtil;
import com.hys.app.mapper.goods.ParametersMapper;
import com.hys.app.model.errorcode.GoodsErrorCode;
import com.hys.app.model.goods.dos.ParameterGroupDO;
import com.hys.app.model.goods.dos.ParametersDO;
import com.hys.app.service.goods.ParameterGroupManager;
import com.hys.app.service.goods.ParametersManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 参数业务类
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018-03-20 16:14:31
 */
@Service
public class ParametersManagerImpl extends ServiceImpl<ParametersMapper, ParametersDO> implements ParametersManager {

	@Autowired
	private ParameterGroupManager parameterGroupManager;
	@Autowired
	private ParametersMapper parametersMapper;
    /**
     * 查询参数列表
     */
	@Override
	public WebPage list(long page, long pageSize) {

		IPage iPage = this.parametersMapper.selectPage(new Page<>(page,pageSize),new QueryWrapper<>());

		return PageConvert.convert(iPage);
	}

	@Override
	@Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ParametersDO add(ParametersDO parameters) {

		// 查询参数组
		ParameterGroupDO group = parameterGroupManager.getModel(parameters.getGroupId());
		if(group == null){
			throw new ServiceException(GoodsErrorCode.E303.code(), "所属参数组不存在");
		}
		parameters.setCategoryId(group.getCategoryId());
		// 选择项
		if(parameters.getParamType() == 2){
			if(!StringUtil.notEmpty(parameters.getOptions()) ){
				throw new ServiceException(GoodsErrorCode.E303.code(), "选择项类型必须填写选择内容");
			}
		}
		ParametersDO paramtmp = this.parametersMapper.selectOne(new QueryWrapper<ParametersDO>()
				.eq("group_id",parameters.getGroupId())
				.orderByDesc("sort")
				.last("limit 0,1"));
		if (paramtmp == null) {
			parameters.setSort(1);
		} else {
			parameters.setSort(paramtmp.getSort() + 1);
		}

		this.parametersMapper.insert(parameters);

		return parameters;
	}
    /**
     * 修改参数
     *
     * @param parameters 参数
     * @param id 参数主键
     * @return Parameters 参数
     */
	@Override
	@Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ParametersDO edit(ParametersDO parameters, Long id) {

		ParametersDO temp = this.getModel(id);
		if(temp == null ){
			throw new ServiceException(GoodsErrorCode.E303.code(), "参数不存在");
		}
		parameters.setCategoryId(temp.getCategoryId());
		// 选择项
		if(parameters.getParamType() == 2){
			if(!StringUtil.notEmpty(parameters.getOptions()) ){
				throw new ServiceException(GoodsErrorCode.E303.code(), "选择项类型必须填写选择内容");
			}
		}

		BeanUtils.copyProperties(parameters,temp);

		this.parametersMapper.updateById(temp);

		return parameters;
	}
    /**
     * 删除参数
     *
     * @param id 参数主键
     */
	@Override
	@Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delete(Long id) {
		this.parametersMapper.deleteById(id);
	}
    /**
     * 获取参数
     * @param id 参数主键
     * @return Parameters 参数
     */
	@Override
	public ParametersDO getModel(Long id) {
		return this.parametersMapper.selectById(id);
	}
    /**
     * 参数排序
     *  @param paramId 参数id
     * @param sortType 上移 up 下移 down
     */
	@Override
	@Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void paramSort(Long paramId, String sortType) {

		String sql = "";

		ParametersDO curParam = this.getModel(paramId);

		if(curParam == null){
			throw new ServiceException(GoodsErrorCode.E303.code(), "要移动的参数不存在");
		}


		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("group_id",curParam.getGroupId());
		if ("up".equals(sortType)) {
			wrapper.lt("sort",curParam.getSort());
			wrapper.orderByDesc("sort");
		} else if ("down".equals(sortType)) {
			wrapper.gt("sort",curParam.getSort());
			wrapper.orderByAsc("sort");
		}
		wrapper.last("limit 0,1");

		ParametersDO changeParam = this.parametersMapper.selectOne(wrapper);
		if (changeParam != null) {

			UpdateWrapper<ParametersDO> updateWrapper = new UpdateWrapper<ParametersDO>()
					.set("sort", changeParam.getSort())
					.eq("param_id", curParam.getParamId());
			this.parametersMapper.update(new ParametersDO(),updateWrapper);

			updateWrapper.set("sort", curParam.getSort())
					.eq("group_id", changeParam.getParamId());
			this.parametersMapper.update(new ParametersDO(),updateWrapper);


		}
	}
    /**
     * 删除参数，使用参数组
     * @param groupId 参数组id
     */
	@Override
	@Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteByGroup(Long groupId) {
		this.parametersMapper.delete(new QueryWrapper<ParametersDO>().eq("group_id",groupId));
	}
}
