package com.zbf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbf.common.annotation.DataSource;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.enums.DataSourceType;
import com.zbf.common.utils.DateUtils;
import com.zbf.common.utils.SecurityUtils;
import com.zbf.system.domain.TCkOrderdetail;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.mapper.TCkOrderdetailMapper;
import com.zbf.system.mapper.TMiStockMapper;
import com.zbf.system.service.ITCkOrderdetailService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 出库物料明细Service业务层处理
 *
 * @author sunits
 * @date 2024-06-21
 */
@Service
public class TCkOrderdetailServiceImpl extends ServiceImpl<TCkOrderdetailMapper, TCkOrderdetail> implements ITCkOrderdetailService //修改
{
    @Autowired
    private TCkOrderdetailMapper tCkOrderdetailMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private TMiStockMapper tMiStockMapper;

    /**
     * 查询出库物料明细
     *
     * @param id 出库物料明细主键
     * @return 出库物料明细
     */
    @Override
    public TCkOrderdetail selectTCkOrderdetailById(String id) {

        return tCkOrderdetailMapper.selectTCkOrderdetailById(id);
    }

    /**
     * 查询出库物料明细列表
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 出库物料明细
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public List<TCkOrderdetail> selectTCkOrderdetailList(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.selectTCkOrderdetailList(tCkOrderdetail);
//        for (TCkOrderdetail tCkOrderdetail1 : list) {
//            TMiStock tMiStock = new TMiStock();
//            tMiStock.setGoodsId(tCkOrderdetail1.getGoodsId());
//            List<TMiStock> tMiStocks = tMiStockMapper.selectList(new QueryWrapper<>(tMiStock));
//            BigDecimal total = BigDecimal.ZERO;
//            for (TMiStock tMiStock1 : tMiStocks) {
//                total = total.add(tMiStock1.getShelvesNum());
//            }
//            tCkOrderdetail1.setStockNum(total);
//        }
//        return list;
    }

    /**
     * 新增出库物料明细
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 结果
     */
    @Override
    public int insertTCkOrderdetail(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.insertTCkOrderdetail(tCkOrderdetail);
    }

    /**
     * 修改出库物料明细
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 结果
     */
    @Override
    public AjaxResult updateTCkOrderdetail(TCkOrderdetail tCkOrderdetail) {
        tCkOrderdetail.setUpdateTime(DateUtils.getNowDate());
        tCkOrderdetail.setGoodsNum(tCkOrderdetail.getShelvesNum());
        int i = tCkOrderdetailMapper.updateTCkOrderdetail(tCkOrderdetail);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 批量删除出库物料明细
     *
     * @param ids 需要删除的出库物料明细主键
     * @return 结果
     */
    @Override
    public int deleteTCkOrderdetailByIds(String[] ids) {
        return tCkOrderdetailMapper.deleteTCkOrderdetailByIds(ids);
    }

    /**
     * 删除出库物料明细信息
     *
     * @param id 出库物料明细主键
     * @return 结果
     */
    @Override
    public int deleteTCkOrderdetailById(String id) {
        return tCkOrderdetailMapper.deleteTCkOrderdetailById(id);
    }


    /**
     * 批量禁用
     *
     * @param ids 出库物料明细主键
     * @return 结果
     */
    @Override
    public int disabledTCkOrderdetailByIds(String[] ids) {
        return tCkOrderdetailMapper.disabledTCkOrderdetailByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param ids 出库物料明细主键
     * @return 结果
     */
    @Override
    public int enableTCkOrderdetailByIds(String[] ids) {
        return tCkOrderdetailMapper.enableTCkOrderdetailByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tCkOrderdetail 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.updateTCkOrderdetail(tCkOrderdetail);
    }

    /**
     * 批量新增出库单物料明细
     *
     * @param tCkOrderdetails 出库单物料明细List
     * @return 结果
     */
    @Override
    public int batchInsertTCkOrderdetail(List<TCkOrderdetail> tCkOrderdetails) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(tCkOrderdetails)) {
            try {
                for (int i = 0; i < tCkOrderdetails.size(); i++) {
                    int row = tCkOrderdetailMapper.insertTCkOrderdetail(tCkOrderdetails.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 3 == 0) || i == tCkOrderdetails.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }


}
