package com.zehong.system.service.impl.track;

import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.system.domain.track.TProductStore;
import com.zehong.system.domain.track.TProductStoreLog;
import com.zehong.system.domain.vo.HouseVo;
import com.zehong.system.mapper.track.TProductStoreMapper;
import com.zehong.system.service.track.ITProductStoreLogService;
import com.zehong.system.service.track.ITProductStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 配件Service业务层处理
 * 
 * @author zehong
 * @date 2024-08-22
 */
@Service
public class TProductStoreServiceImpl implements ITProductStoreService 
{
    @Autowired
    private TProductStoreMapper tProductStoreMapper;

    @Resource
    private ITProductStoreLogService itProductStoreLogService;

    /**
     * 查询配件
     * 
     * @param id 配件ID
     * @return 配件
     */
    @Override
    public TProductStore selectTProductStoreById(Long id)
    {
        return tProductStoreMapper.selectTProductStoreById(id);
    }

    /**
     * 查询配件列表
     * 
     * @param tProductStore 配件
     * @return 配件
     */
    @Override
    public List<TProductStore> selectTProductStoreList(TProductStore tProductStore)
    {
        return tProductStoreMapper.selectTProductStoreList(tProductStore);
    }

    /**
     * 新增配件
     * 
     * @param tProductStore 配件
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTProductStore(TProductStore tProductStore)
    {
        tProductStore.setCreateTime(DateUtils.getNowDate());
        tProductStore.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        tProductStoreMapper.insertTProductStore(tProductStore);
        TProductStoreLog log = new TProductStoreLog();
        log.setNumber(tProductStore.getTotal());
        log.setStoreId(tProductStore.getId());
        return itProductStoreLogService.insertTProductStoreLog(log);
    }

    /**
     * 修改配件
     * 
     * @param tProductStore 配件
     * @return 结果
     */
    @Override
    public int updateTProductStore(TProductStore tProductStore)
    {
        return tProductStoreMapper.updateTProductStore(tProductStore);
    }

    /**
     * 批量删除配件
     * 
     * @param ids 需要删除的配件ID
     * @return 结果
     */
    @Override
    public int deleteTProductStoreByIds(Long[] ids)
    {
        return tProductStoreMapper.deleteTProductStoreByIds(ids);
    }

    /**
     * 删除配件信息
     * 
     * @param id 配件ID
     * @return 结果
     */
    @Override
    public int deleteTProductStoreById(Long id)
    {
        return tProductStoreMapper.deleteTProductStoreById(id);
    }

    /**
     * 配件批量入库
     * @param storeInfo 配件信息
     * @param isAddLog 是否插入日志
     * @param type 操作类型
     * @return
     */
    @Override
    public int computeStock(List<HouseVo> storeInfo,boolean isAddLog,String type){
        synchronized (this){
            if("add".equals(type)){
                addStoreStock(storeInfo,isAddLog);
            }else{
                subtractStoreStock(storeInfo);
            }
        }
        return 1;
    }


    /**
     * 增加库存
     * @param storeInfo
     * @param isAddLog
     */
    @Transactional(rollbackFor = Exception.class)
    public void addStoreStock(List<HouseVo> storeInfo,boolean isAddLog){
        for(HouseVo house : storeInfo){
            TProductStore store = tProductStoreMapper.selectTProductStoreById(house.getId());
            store.setTotal(store.getTotal() + house.getNum());
            tProductStoreMapper.updateTProductStore(store);
            if(isAddLog){
                TProductStoreLog log = new TProductStoreLog();
                log.setNumber(house.getNum());
                log.setStoreId(house.getId());
                itProductStoreLogService.insertTProductStoreLog(log);
            }
        }
    }

    /**
     * 扣出库存
     * @param storeList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void subtractStoreStock(List<HouseVo> storeList){
        for(HouseVo storeInfo : storeList){
            TProductStore store = tProductStoreMapper.selectTProductStoreById(storeInfo.getId());
            if(store.getTotal().compareTo(storeInfo.getNum()) == -1){
                throw new CustomException(store.getTitle() + "库存不足");
            }
            store.setTotal(store.getTotal() - storeInfo.getNum());
            tProductStoreMapper.updateTProductStore(store);
        }
    }
}
