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

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.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataCreateReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataRespVO;
import cn.iocoder.yudao.module.smartfactory.convert.packageData.PackageDataConvert;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydata.FactoryDataDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.packagedata.PackageDataDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.packagedata.PackageDataMapper;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

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


/**
 * 打包线数据 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class PackageDataServiceImpl implements PackageDataService {

    @Resource
    private PackageDataMapper packageDataMapper;

    @Resource
    @Lazy // 循环依赖，避免报错
    private FactoryDataService factoryDataService;

    @Override
    public Long createPackageData(PackageDataCreateReqVO createReqVO) {

        //校验 当前日期的数据是否已经录入
        List<PackageDataDO> list = packageDataMapper.selectListByFactoryIdAndDate(createReqVO.getFactoryId(), createReqVO.getPackageDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        if (list != null && !list.isEmpty()) {
            throw exception(PACKAGE_DATA_EXISTS);
        }

        //校验 当前日期的入库数据是否已经统计过
        FactoryDataDO factoryDataDO = factoryDataService.getListByType(createReqVO.getFactoryId(), createReqVO.getPackageDate(), 2);
        if (factoryDataDO != null) {
            throw exception(PACKAGE_DATA_EXISTS);
        }

        // 插入
        List<PackageDataDO> packageData = PackageDataConvert.INSTANCE.convertList(createReqVO);

        packageDataMapper.insertBatch(packageData);

        // 返回
        return (long) packageData.size();
    }

    @Override
    public void updatePackageData(PackageDataCreateReqVO updateReqVO) {

        // 校验是否为当天录入得数据
        validateDataExists(updateReqVO.getFactoryId(), updateReqVO.getPackageDate());

        // 更新
        List<PackageDataDO> updateObj = BeanUtils.toBean(updateReqVO.getPackageData(), PackageDataDO.class);

        packageDataMapper.updateBatch(updateObj);
    }

    @Override
    public void deletePackageData(Long factoryId, String date) {

        // 校验是否为当天录入得数据
        validateDataExists(factoryId, LocalDate.parse(date));

        // 删除
        packageDataMapper.delectData(factoryId, date);
    }

    private void validateDataExists(Long factoryId, LocalDate packageDate) {

        LocalDateTime[] dateTimes = {LocalDateTime.now().with(LocalTime.MIN), LocalDateTime.now().with(LocalTime.MAX)};
        Long count = packageDataMapper.selectCountByDateTime(factoryId, packageDate, dateTimes);
        if (count == 0L) {
            throw exception(SIZEDATA_NOT_EXISTS);
        }
    }

    @Override
    public PageResult<PackageDataRespVO> getPackageDataPage(PackageDataPageReqVO pageReqVO) {

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

    @Override
    public List<PackageDataDO> getFactoryPackLineInfo(Long factoryId) {
        return packageDataMapper.selectList(new LambdaQueryWrapperX<PackageDataDO>().eq(PackageDataDO::getFactoryId, factoryId));
    }

    @Override
    public List<PackageDataDO> getFactoryPackLineInfoByTime(Long factoryId, Date beginTime, Date endTime) {
        return packageDataMapper.selectList(new LambdaQueryWrapperX<PackageDataDO>().eq(PackageDataDO::getFactoryId, factoryId)
                .ge(PackageDataDO::getCreateTime, beginTime)
                .le(PackageDataDO::getCreateTime, endTime));
    }

    @Override
    public List<FactoryDataDO> getDataListByDateTime(LocalDateTime[] dateTimes) {

        return packageDataMapper.selectDataListByDateTime(dateTimes);
    }

    @Override
    public List<PackageDataDO> getListByFactoryIdAndDate(Long factoryId, String date) {

        return packageDataMapper.selectListByFactoryIdAndDate(factoryId, date);
    }
}