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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.mapper.goods.SpecValuesMapper;
import com.hys.app.model.errorcode.GoodsErrorCode;
import com.hys.app.model.goods.dos.SpecValuesDO;
import com.hys.app.model.goods.dos.SpecificationDO;
import com.hys.app.model.goods.enums.Permission;
import com.hys.app.service.goods.SpecValuesManager;
import com.hys.app.service.goods.SpecificationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 规格值业务类
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0
 * 2018-03-20 10:23:53
 */
@Service
public class SpecValuesManagerImpl implements SpecValuesManager {

    @Autowired
    private SpecValuesMapper specValuesMapper;
    @Autowired
    private SpecificationManager specificationManager;
    /**
     * 添加规格值
     *
     * @param specValues
     *            规格值
     * @return SpecValues 规格值
     */
    @Override
    @Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SpecValuesDO add(SpecValuesDO specValues) {

        this.specValuesMapper.insert(specValues);

        return specValues;
    }
    /**
     * 修改规格值
     *
     * @param specValues
     *            规格值
     * @param id
     *            规格值主键
     * @return SpecValues 规格值
     */
    @Override
    @Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SpecValuesDO edit(SpecValuesDO specValues, Long id) {

        specValues.setSpecId(id);
        this.specValuesMapper.updateById(specValues);
        return specValues;
    }
    /**
     * 获取规格值
     *
     * @param id
     *            规格值主键
     * @return SpecValues 规格值
     */
    @Override
    public SpecValuesDO getModel(Long id) {
        return this.specValuesMapper.selectById(id);
    }
    /**
     * 获取某规格的规格值
     *
     * @param specId 规格id
     * @param permission 权限
     * @return
     */
    @Override
    public List<SpecValuesDO> listBySpecId(Long specId, Permission permission) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("spec_id",specId);

        //商家或者后台调用
        if (Permission.ADMIN.equals(permission)) {
            queryWrapper.eq("seller_id",0);
        }

        List<SpecValuesDO> list = this.specValuesMapper.selectList(queryWrapper);

        return list;
    }
    /**
     * 添加某规格的规格值
     *
     * @param specId 规格id
     * @param valueList 规格值集合
     * @return
     */
    @Override
    @Transactional(value = "goodsTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<SpecValuesDO> saveSpecValue(Long specId, String[] valueList) {

        //查询规格是否存在
        SpecificationDO spec = specificationManager.getModel(specId);
        if (spec == null) {
            throw new ServiceException(GoodsErrorCode.E306.code(), "所属规格不存在");
        }

        this.specValuesMapper.delete(new QueryWrapper<SpecValuesDO>()
                .eq("spec_id",specId)
                .eq("seller_id",0));

        List<SpecValuesDO> res = new ArrayList<>();
        for (String value : valueList) {
            if (value.length() > 50) {
                throw new ServiceException(GoodsErrorCode.E305.code(), "规格值为1到50个字符之间");
            }
            SpecValuesDO specValue = new SpecValuesDO(specId, value, 0L);
            specValue.setSpecName(spec.getSpecName());
            this.specValuesMapper.insert(specValue);
            res.add(specValue);
        }
        return res;

    }
}
