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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screendata.factory.vo.PropertyDataRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screenfactoryextra.vo.ScreenFactoryExtraPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screenfactoryextra.vo.ScreenFactoryExtraSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydataoverview.FactoryDataOverviewDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.screenfactoryextra.ScreenFactoryExtraDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.factorydataoverview.FactoryDataOverviewMapper;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.screenfactoryextra.ScreenFactoryExtraMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.FACTORY_INFO_NOT_EXISTS;

/**
 * 工厂大屏额外字段 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class ScreenFactoryExtraServiceImpl implements ScreenFactoryExtraService {

    @Resource
    private ScreenFactoryExtraMapper screenFactoryExtraMapper;
    @Resource
    private FactoryDataOverviewMapper factoryDataOverviewMapper;

    @Override
    public Long createScreenFactoryExtra(ScreenFactoryExtraSaveReqVO createReqVO) {
        // 插入
        ScreenFactoryExtraDO screenFactoryExtra = BeanUtils.toBean(createReqVO, ScreenFactoryExtraDO.class);
        screenFactoryExtraMapper.insert(screenFactoryExtra);
        // 返回
        return screenFactoryExtra.getId();
    }

    @Override
    public void updateScreenFactoryExtra(ScreenFactoryExtraSaveReqVO updateReqVO) {
        // 校验存在
        validateScreenFactoryExtraExists(updateReqVO.getId());
        // 更新
        ScreenFactoryExtraDO updateObj = BeanUtils.toBean(updateReqVO, ScreenFactoryExtraDO.class);
        screenFactoryExtraMapper.updateById(updateObj);
    }

    @Override
    public void deleteScreenFactoryExtra(Long id) {
        // 校验存在
        validateScreenFactoryExtraExists(id);
        // 删除
        screenFactoryExtraMapper.deleteById(id);
    }

    private void validateScreenFactoryExtraExists(Long id) {
        if (screenFactoryExtraMapper.selectById(id) == null) {
            throw exception(FACTORY_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ScreenFactoryExtraDO getScreenFactoryExtra(Long id) {
        return screenFactoryExtraMapper.selectById(id);
    }

    @Override
    public PageResult<ScreenFactoryExtraDO> getScreenFactoryExtraPage(ScreenFactoryExtraPageReqVO pageReqVO) {
        return screenFactoryExtraMapper.selectPage(pageReqVO);
    }

    @Override
    public PropertyDataRespVO getPropertyData(Long factoryId) {
        PropertyDataRespVO vo = new PropertyDataRespVO();
        ScreenFactoryExtraDO screenFactoryExtraDO = screenFactoryExtraMapper.selectOne(new LambdaQueryWrapperX<ScreenFactoryExtraDO>()
                .eq(ScreenFactoryExtraDO::getFactoryId, factoryId));

        FactoryDataOverviewDO factoryDataOverviewDO = factoryDataOverviewMapper.selectOne(new LambdaQueryWrapperX<FactoryDataOverviewDO>()
                .eq(FactoryDataOverviewDO::getFactoryId, factoryId));
        if (factoryDataOverviewDO != null) {
            vo.setPackLineTotal(factoryDataOverviewDO.getDbxNum());
            vo.setTrayTotal(factoryDataOverviewDO.getTpNum());
            vo.setTieTotal(factoryDataOverviewDO.getBdNum());
            vo.setForkliftTotal(factoryDataOverviewDO.getTruckTotalNum());
        }
        if (screenFactoryExtraDO != null) {
            vo.setKilnTotal(screenFactoryExtraDO.getYlNum());
            vo.setOnlineTotal(screenFactoryExtraDO.getTruckOnNum());
            vo.setOutLineTotal(screenFactoryExtraDO.getTruckOffNum());
        }
        return vo;
    }

    @Override
    public List<ScreenFactoryExtraDO> list() {
        return screenFactoryExtraMapper.selectList();
    }

}