package com.cf.imes.module.executor.service.goods;

import com.cf.imes.module.executor.dal.dataobject.managePlate.ManagePlateDO;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import com.cf.imes.module.executor.dal.mysql.managePlate.ManagePlateMapper;
import com.cf.imes.module.executor.dal.mysql.order.OrderMapper;
import com.cf.imes.module.executor.service.plate.PlateService;
import com.cf.imes.module.executor.service.rawgoods.RawGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.cf.imes.module.executor.controller.admin.goods.vo.*;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;

import com.cf.imes.module.executor.dal.mysql.goods.GoodsMapper;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;

/**
 * 生产单商品 Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private PlateService plateService;

    @Resource
    private RawGoodsService rawGoodsService;

    @Autowired
    private ManagePlateMapper plateMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createCorrespondsGoods(List<GoodsSaveReqVO> createReqVOS) {
        List<Long> longs = new ArrayList<>();
        // 校验生产单是否存在
        if (orderMapper.selectOne(OrderDO::getId, createReqVOS.get(0).getOrderId()) == null) {
            throw exception(GOODS_NOT_EXISTS);
        }
        createReqVOS.forEach(createReqVO -> {
            // 校验商品是否存在
            if (plateMapper.selectOne(ManagePlateDO::getId, createReqVO.getGoodsId()) == null) {
                throw exception(PLATE_NOT_EXISTS);
            }
            // 校验
            if (rawGoodsService.getRawGoods(createReqVO.getRawGoodsId()) == null) {
                throw exception(RAW_GOODS_NOT_EXISTS);
            }
        });
        // 一次性插入数据库
        goodsMapper.insertBatch(BeanUtils.toBean(createReqVOS, GoodsDO.class));
        // 修改order_plate
        plateService.updatePlateByRawGoodsId(createReqVOS);
        // 返回
        return true;
    }

    @Override
    public void updateGoods(GoodsSaveReqVO updateReqVO) {
        // 校验存在
        validateGoodsExists(updateReqVO.getId());
        // 更新
        GoodsDO updateObj = BeanUtils.toBean(updateReqVO, GoodsDO.class);
        goodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteGoods(Long id) {
        // 校验存在
        validateGoodsExists(id);
        // 删除
        goodsMapper.deleteById(id);
    }

    private void validateGoodsExists(Long id) {
        if (goodsMapper.selectById(id) == null) {
            throw exception(GOODS_NOT_EXISTS);
        }
    }

    @Override
    public GoodsDO getGoods(Long id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public PageResult<GoodsDO> getGoodsPage(GoodsPageReqVO pageReqVO) {
        return goodsMapper.selectPage(pageReqVO);
    }

    @Override
    public void getCounterpartNewGoods(Set<GoodsSaveReqVO> createReqVOS) {
        // 校验存在
        createReqVOS.forEach(createReqVO -> {

        });
    }

}