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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
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.factorydata.vo.FactoryDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydata.FactoryDataDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.factorydata.FactoryDataMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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


/**
 * 工厂出库入库数据 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class FactoryDataServiceImpl implements FactoryDataService {

    @Resource
    private FactoryDataMapper dataMapper;

    @Override
    public Long createData(FactoryDataSaveReqVO createReqVO) {
        // 插入
        FactoryDataDO data = BeanUtils.toBean(createReqVO, FactoryDataDO.class);
        dataMapper.insert(data);
        // 返回
        return data.getId();
    }

    @Override
    public void updateData(FactoryDataSaveReqVO updateReqVO) {
        // 更新
        FactoryDataDO updateObj = BeanUtils.toBean(updateReqVO, FactoryDataDO.class);
        dataMapper.updateById(updateObj);
    }

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

    @Override
    public FactoryDataDO getData(Long id) {
        return dataMapper.selectById(id);
    }

    @Override
    public PageResult<FactoryDataDO> getDataPage(FactoryDataPageReqVO pageReqVO) {
        return dataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FactoryDataDO> getTodayInboundAndOutboundOverview(Date time) {
        String timeStr = null;
        if (time != null) {
            timeStr = DateUtils.dateFormat(time, "yyyy-MM-dd");
        }
        return dataMapper.selectList(new LambdaQueryWrapperX<FactoryDataDO>()
                .eq(timeStr != null, FactoryDataDO::getDate, timeStr));
    }

    @Override
    public List<FactoryDataDO> getByTimeAndFactoryId(Long factoryId, Date beginDay, Date endDay) {
        return dataMapper.selectList(new LambdaQueryWrapperX<FactoryDataDO>()
                .ge(FactoryDataDO::getDate, beginDay)
                .le(FactoryDataDO::getDate, endDay)
                .eq(FactoryDataDO::getFactoryId, factoryId));
    }

}