package com.yc.ship.module.product.service.voyagestock;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.module.resource.api.ship.ShipApi;
import com.yc.ship.module.resource.api.ship.dto.ShipRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

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

import com.yc.ship.module.product.controller.admin.voyagestock.vo.*;
import com.yc.ship.module.product.dal.dataobject.voyagestock.VoyageStockDO;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;

import com.yc.ship.module.product.dal.mysql.voyagestock.VoyageStockMapper;

import java.util.List;
import java.util.Map;


/**
 * 航次库存 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class VoyageStockServiceImpl implements VoyageStockService {

    @Resource
    private VoyageStockMapper voyageStockMapper;


    @Resource
    private ShipApi shipApi;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long createVoyageStock(VoyageStockSaveReqVO createReqVO) {
        // 插入
        VoyageStockDO voyageStock = BeanUtils.toBean(createReqVO, VoyageStockDO.class);
        voyageStockMapper.insert(voyageStock);
        Long voyageId = voyageStock.getVoyageId();
        // 返回
        return voyageId;
    }
    @Override
    public void updateVoyageStock(VoyageStockSaveReqVO updateReqVO) {
        // 更新
        VoyageStockDO updateObj = BeanUtils.toBean(updateReqVO, VoyageStockDO.class);
        voyageStockMapper.updateById(updateObj);
    }

    @Override
    public void deleteVoyageStock(Long id) {
        // 删除
        voyageStockMapper.deleteById(id);
    }


    @Override
    public VoyageStockDO getVoyageStock(Long id) {
        return voyageStockMapper.selectById(id);
    }

    @Override
    public PageResult<VoyageStockRespVO> getVoyageStockPage(VoyageStockPageReqVO pageReqVO) {
        String[] voyageTime = pageReqVO.getVoyageTime();
        if(voyageTime != null && voyageTime.length > 0) {
            voyageTime[1] += " 23:59:59";
        }
        PageResult<VoyageStockDO> pageResult = voyageStockMapper.selectPage(pageReqVO);
        PageResult<VoyageStockRespVO> page = BeanUtils.toBean(pageResult, VoyageStockRespVO.class);
        List<VoyageStockRespVO> list = page.getList();
        List<Long> shipIds = CollectionUtils.convertList(list, VoyageStockRespVO::getShipId);
        Map<Long, ShipRespDTO> shipMap = shipApi.queryShip(shipIds);
        list.stream().forEach(item -> {
            MapUtils.findAndThen(shipMap, item.getShipId(), ship -> item.setShipName(ship.getName()));
        });
        return page;
    }

    @Override
    public VoyageStockTotalVO getVoyageStockTotal(VoyageStockPageReqVO pageReqVO) {
        String[] voyageTime = pageReqVO.getVoyageTime();
        if(voyageTime != null && voyageTime.length > 0) {
            voyageTime[1] += " 23:59:59";
        }
        return voyageStockMapper.selectTotal(pageReqVO);
    }

}