package cn.iocoder.yudao.module.smartfactory.service.packageInfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackagePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackageRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packageInfo.vo.PackageSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.packageInfo.PackageDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.size.SizeDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.packageInfo.PackageMapper;
import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService;
import cn.iocoder.yudao.module.smartfactory.service.size.SizeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
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.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.PACKAGE_NOT_EXISTS;

/**
 * 打包线 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class PackageServiceImpl implements PackageService {

    @Resource
    private PackageMapper packageMapper;

    @Resource
    private FactoryInfoService factoryService;

    @Resource
    private SizeService sizeService;

    @Override
    public Long createPackage(PackageSaveReqVO createReqVO) {
        // 插入
        PackageDO packDO = BeanUtils.toBean(createReqVO, PackageDO.class);
        packageMapper.insert(packDO);
        // 返回
        return packDO.getId();
    }

    @Override
    public void updatePackage(PackageSaveReqVO updateReqVO) {
        // 校验存在
        validatePackageExists(updateReqVO.getId());
        // 更新
        PackageDO updateObj = BeanUtils.toBean(updateReqVO, PackageDO.class);
        packageMapper.updateById(updateObj);
    }

    @Override
    public void deletePackage(Long id) {
        // 校验存在
        validatePackageExists(id);
        // 删除
        packageMapper.deleteById(id);
    }

    private void validatePackageExists(Long id) {
        if (packageMapper.selectById(id) == null) {
            throw exception(PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public PackageDO getPackage(Long id) {

        return packageMapper.selectById(id);
    }

    @Override
    public List<PackageRespVO> getPackageByFactoryId(Long factoryId) {

        //获得工厂信息
        FactoryInfoDO factoryInfoDO = factoryService.getFactoryInfo(factoryId);

        //获得打包线信息列表
        List<PackageDO> packageDOS = packageMapper.selectList(PackageDO::getFactoryId, factoryId);
        //获得 规格信息
        List<Long> sizeIds = convertList(packageDOS, PackageDO::getSizeId);
        List<SizeDO> sizeDOS = sizeService.getSizes(sizeIds);

        //获得规格信息Map
        Map<Long, SizeDO> sizeMap = convertMap(sizeDOS, SizeDO::getId);

        List<PackageRespVO> respVOS = BeanUtils.toBean(packageDOS, PackageRespVO.class);
        respVOS.forEach(data -> {

            data.setFactoryName(factoryInfoDO.getName());
            data.setSizeName(sizeMap.get(data.getSizeId()).getName());
        });

        return respVOS;
    }

    @Override
    public PageResult<PackageRespVO> getPackagePage(PackagePageReqVO pageReqVO) {

        IPage<PackageRespVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        IPage<PackageRespVO> page = packageMapper.selectPackagePage(mpPage, pageReqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }
}