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

import cn.hutool.core.collection.CollUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsImportRespVO;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsPageReqVO;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsSaveReqVO;
import com.cf.imes.module.system.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.cf.imes.module.executor.dal.dataobject.rawgoods.RawGoodsDO;
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.rawgoods.RawGoodsMapper;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.RAW_GOODS_NOT_EXISTS;

/**
 * 生产单设计商品表 order_raw_goods_{N} Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class RawGoodsServiceImpl implements RawGoodsService {

    @Resource
    private RawGoodsMapper rawGoodsMapper;

    @Override
    public Long createRawGoods(RawGoodsSaveReqVO createReqVO) {
        // 插入
        RawGoodsDO rawGoods = BeanUtils.toBean(createReqVO, RawGoodsDO.class);
        rawGoodsMapper.insert(rawGoods);
        // 返回
        return rawGoods.getId();
    }

    @Override
    public void updateRawGoods(RawGoodsSaveReqVO updateReqVO) {
        // 校验存在
        validateRawGoodsExists(updateReqVO.getId());
        // 更新
        RawGoodsDO updateObj = BeanUtils.toBean(updateReqVO, RawGoodsDO.class);
        rawGoodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteRawGoods(Long id) {
        // 校验存在
        validateRawGoodsExists(id);
        // 删除
        rawGoodsMapper.deleteById(id);
    }

    private void validateRawGoodsExists(Long id) {
        if (rawGoodsMapper.selectById(id) == null) {
            throw exception(RAW_GOODS_NOT_EXISTS);
        }
    }

    @Override
    public RawGoodsDO getRawGoods(Long id) {
        return rawGoodsMapper.selectById(id);
    }

    @Override
    public PageResult<RawGoodsDO> getRawGoodsPage(RawGoodsPageReqVO pageReqVO) {
        return rawGoodsMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public RawGoodsImportRespVO importRawGoodsList(List<RawGoodsSaveReqVO> importRawGoods, Long orderId) {
        if (CollUtil.isEmpty(importRawGoods)) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.RAW_GOODS_IMPORT_LIST_IS_EMPTY);
        }
        RawGoodsImportRespVO respVO = RawGoodsImportRespVO.builder().createRawGoods(new ArrayList<>())
                .updateRawGoods(new ArrayList<>()).failureRawGoods(new LinkedHashMap<>()).build();
        importRawGoods.forEach(rawGoodsSaveReqVO -> {
            try{
                rawGoodsMapper.insert(BeanUtils.toBean(rawGoodsSaveReqVO, RawGoodsDO.class)
                        .setOrderId(orderId));
                respVO.getCreateRawGoods().add(rawGoodsSaveReqVO.getGoodsName());
            }catch (ServiceException ex){
                respVO.getFailureRawGoods().put(rawGoodsSaveReqVO.getGoodsName(), ex.getMessage());
            }

        });
        return respVO;
    }

    @Override
    public List<RawGoodsDO> getRawGoodsByOrder(Long orderId) {
        return rawGoodsMapper.selectList(RawGoodsDO::getOrderId, orderId,RawGoodsDO::getOrganId,getUserOrganId());
    }
}