package com.fjhx.wms.service.stock.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.wms.entity.stock.dto.StockSnapshotSelectDto;
import com.fjhx.wms.entity.stock.po.Stock;
import com.fjhx.wms.entity.stock.po.StockSnapshot;
import com.fjhx.wms.entity.stock.po.StockSnapshotDetails;
import com.fjhx.wms.entity.stock.vo.StockSnapshotVo;
import com.fjhx.wms.mapper.stock.StockSnapshotMapper;
import com.fjhx.wms.service.stock.StockService;
import com.fjhx.wms.service.stock.StockSnapshotDetailsService;
import com.fjhx.wms.service.stock.StockSnapshotService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 库存快照 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-17
 */
@Service
public class StockSnapshotServiceImpl extends ServiceImpl<StockSnapshotMapper, StockSnapshot> implements StockSnapshotService {

    @Autowired
    private StockService stockService;
    @Autowired
    private StockSnapshotDetailsService stockSnapshotDetailsService;

    @Override
    public Page<StockSnapshotVo> getPage(StockSnapshotSelectDto dto) {
        IWrapper<StockSnapshot> wrapper = getWrapper();
        wrapper.orderByDesc("ss", StockSnapshot::getId);
        Page<StockSnapshotVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    // @PostConstruct
    @Scheduled(cron = "0 0 0 * * ?")
    @DSTransactional
    public void stockSnapshot() {
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        SecurityUtils.setTenantId("000000");

        //获取前一天的结束时间
        Date dateTime = DateUtil.endOfDay(DateUtil.yesterday()).offset(DateField.MILLISECOND, -999);
        StockSnapshot stockSnapshot = new StockSnapshot();
        stockSnapshot.setSnapshotTime(dateTime);
        this.save(stockSnapshot);

        //快照库存
        List<Stock> stockList = stockService.list();
        List<StockSnapshotDetails> stockSnapshotDetails = BeanUtil.copyToList(stockList, StockSnapshotDetails.class);
        for (StockSnapshotDetails stockSnapshotDetail : stockSnapshotDetails) {
            stockSnapshotDetail.setSnapshotId(stockSnapshot.getId());
            stockSnapshotDetail.setStockId(stockSnapshotDetail.getId());
            stockSnapshotDetail.setId(null);
        }
        stockSnapshotDetailsService.saveBatch(stockSnapshotDetails);

        SecurityUtils.clearTenantId();
        DynamicDataSourceContextHolder.poll();
    }

}
