package cn.iocoder.yudao.module.mes.service.stockworkshop;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductUnitDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductService;
import cn.iocoder.yudao.module.erp.service.product.ErpProductUnitService;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.stockworkshop.StockWorkshopDetailDO;
import cn.iocoder.yudao.module.mes.dal.mysql.stockworkshop.StockWorkshopDetailMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.STOCK_WORKSHOP_DETAIL_NOT_EXISTS;

/**
 * 车间仓明细 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class StockWorkshopDetailServiceImpl implements StockWorkshopDetailService {

    @Resource
    private StockWorkshopDetailMapper stockWorkshopDetailMapper;
    @Resource
    private ErpProductService productService;
    @Resource
    private ErpProductUnitService productUnitService;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createStockWorkshopDetail(StockWorkshopDetailSaveReqVO createReqVO) {
        // 插入
        StockWorkshopDetailDO stockWorkshopDetail = BeanUtils.toBean(createReqVO, StockWorkshopDetailDO.class);
        stockWorkshopDetailMapper.insert(stockWorkshopDetail);
        // 返回
        return stockWorkshopDetail.getId();
    }

    @Override
    public void updateStockWorkshopDetail(StockWorkshopDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateStockWorkshopDetailExists(updateReqVO.getId());
        // 更新
        StockWorkshopDetailDO updateObj = BeanUtils.toBean(updateReqVO, StockWorkshopDetailDO.class);
        stockWorkshopDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteStockWorkshopDetail(Long id) {
        // 校验存在
        validateStockWorkshopDetailExists(id);
        // 删除
        stockWorkshopDetailMapper.deleteById(id);
    }

    private void validateStockWorkshopDetailExists(Long id) {
        if (stockWorkshopDetailMapper.selectById(id) == null) {
            throw exception(STOCK_WORKSHOP_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public StockWorkshopDetailDO getStockWorkshopDetail(Long id) {
        return stockWorkshopDetailMapper.selectById(id);
    }

    @Override
    public PageResult<StockWorkshopDetailDO> getStockWorkshopDetailPage(StockWorkshopDetailPageReqVO pageReqVO) {
        return stockWorkshopDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StockWorkshopDetailRespVO> buildVOList(List<StockWorkshopDetailDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<Long, ErpProductDO> map = productService.getProductMap(
                convertSet(list, StockWorkshopDetailDO::getItemId));
        Map<Long, ErpProductUnitDO> unitMap = productUnitService.getProductUnitMap(
                convertSet(list, StockWorkshopDetailDO::getUnitId));
        // 1.4 管理员信息
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertSet(list, StockWorkshopDetailDO::getUserId));

        return BeanUtils.toBean(list, StockWorkshopDetailRespVO.class, item -> {
            MapUtils.findAndThen(map, item.getItemId(),
                    product -> item.setItemName(product.getName()));
            MapUtils.findAndThen(unitMap, item.getUnitId(),
                    unit -> item.setUnitName(unit.getName()));
            MapUtils.findAndThen(userMap, Long.valueOf(item.getCreator()),
                    user -> item.setUserName(user.getNickname()));
        });
    }

}