package com.younike.system.service.impl;

import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.system.domain.GoodsSpecification;
import com.younike.system.domain.Order;
import com.younike.system.mapper.GoodsSpecificationMapper;
import com.younike.system.param.GoodsSpecificationAddParam;
import com.younike.system.service.IGoodsSpecificationService;
import com.younike.system.service.IOrderService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 团购商品规格Service业务层处理
 * 
 * @author dail
 * @date 2022-03-11
 */
@Service
public class GoodsSpecificationServiceImpl implements IGoodsSpecificationService {
    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    private IOrderService orderService;

    /**
     * 查询团购商品规格
     * 
     * @param goodsSpecificationId 团购商品规格主键
     * @return 团购商品规格
     */
    @Override
    public GoodsSpecification selectGoodsSpecificationByGoodsSpecificationId(Long goodsSpecificationId) {
        return goodsSpecificationMapper.selectGoodsSpecificationByGoodsSpecificationId(goodsSpecificationId);
    }

    /**
     * 查询团购商品规格列表
     * 
     * @param goodsSpecification 团购商品规格
     * @return 团购商品规格
     */
    @Override
    public List<GoodsSpecification> selectGoodsSpecificationList(GoodsSpecification goodsSpecification) {
        return goodsSpecificationMapper.selectGoodsSpecificationList(goodsSpecification);
    }

    /**
     * 新增团购商品规格
     * 
     * @param goodsSpecification 团购商品规格
     * @return 结果
     */
    @Override
    public int insertGoodsSpecification(GoodsSpecification goodsSpecification) {
        goodsSpecification.setCreateTime(LocalDateTime.now());
        return goodsSpecificationMapper.insertGoodsSpecification(goodsSpecification);
    }

    /**
     * 修改团购商品规格
     * 
     * @param goodsSpecification 团购商品规格
     * @return 结果
     */
    @Override
    public int updateGoodsSpecification(GoodsSpecification goodsSpecification) {
        goodsSpecification.setUpdateTime(LocalDateTime.now());
        return goodsSpecificationMapper.updateGoodsSpecification(goodsSpecification);
    }

    /**
     * 批量删除团购商品规格
     * 
     * @param goodsSpecificationIds 需要删除的团购商品规格主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpecificationByGoodsSpecificationIds(Long[] goodsSpecificationIds) {
        return goodsSpecificationMapper.deleteGoodsSpecificationByGoodsSpecificationIds(goodsSpecificationIds);
    }

    /**
     * 删除团购商品规格信息
     * 
     * @param goodsSpecificationId 团购商品规格主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpecificationByGoodsSpecificationId(Long goodsSpecificationId)
    {
        return goodsSpecificationMapper.deleteGoodsSpecificationByGoodsSpecificationId(goodsSpecificationId);
    }

    /**
     * 批量新增商品规格信息
     *
     * @param goodsSpecificationList
     */
    @Override
    public void insertBatch(List<GoodsSpecification> goodsSpecificationList) {
        goodsSpecificationMapper.insertBatch(goodsSpecificationList);
    }

    /**
     * 根据商品id删除商品规格信息
     *
     * @param goodsId
     */
    @Override
    public void removeGoodsSpecificationByGoodsId(Long goodsId) {
        // 校验是否存在尚未处理完的服务订单， 若存在，则不能删除
        List<Order> pendingDealOrderList = orderService.adminDeleteGoods(goodsId);
        if (CollectionUtils.isNotEmpty(pendingDealOrderList)) {
            return;
        }
        goodsSpecificationMapper.removeGoodsSpecificationByGoodsId(goodsId);
    }

    /**
     * 更新商品规格信息
     *
     * @param goodsId
     * @param goodsSpecificationList
     */
    @Override
    public void changeGoodsSpecification(Long goodsId, List<GoodsSpecificationAddParam> goodsSpecificationList) {
        if (CollectionUtils.isEmpty(goodsSpecificationList)){
            return;
        }
        // 删除商品原规格信息
        this.removeGoodsSpecificationByGoodsId(goodsId);

        // 获取当前登录用户信息
        Long currentUser = SecurityUtils.getUserId();
        // 组装数据
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationList.stream().map(goodsSpecificationAddParam -> {
            GoodsSpecification goodsSpecification = new GoodsSpecification();
            BeanUtils.copyBeanProp(goodsSpecification, goodsSpecificationAddParam);
            goodsSpecification.setGoodsId(goodsId);
            goodsSpecification.setCreateBy(currentUser == null ? "-" : currentUser.toString());
            goodsSpecification.setUpdateBy(currentUser == null ? "-" : currentUser.toString());
            return goodsSpecification;
        }).collect(toList());

        // 执行批量保存
        this.insertBatch(goodsSpecifications);
    }

    /**
     * 根据商品id列表查询商品规格信息
     *
     * @param goodsIds
     * @return
     */
    @Override
    public List<GoodsSpecification> listByGoodsIds(List<Long> goodsIds) {
        return goodsSpecificationMapper.listByGoodsIds(goodsIds);
    }
}
