package com.ruoyi.basicData.service.impl;

import com.ruoyi.basicData.domain.TWarehouseArea;
import com.ruoyi.basicData.mapper.TWarehouseAreaMapper;
import com.ruoyi.basicData.mapper.TWarehouseMapper;
import com.ruoyi.basicData.service.ITWarehouseAreaService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.TWarehouse;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.mapper.TWarehouseBillsMapper;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * 库区Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TWarehouseAreaServiceImpl implements ITWarehouseAreaService {
    @Autowired
    private TWarehouseAreaMapper tWarehouseAreaMapper;

    @Autowired
    private TWarehousebillsitemsMapper tWarehousebillsitemsMapper;

    @Autowired
    private TWarehouseMapper tWarehouseMapper;

    /**
     * 查询库区
     *
     * @param fId 库区ID
     * @return 库区
     */
    @Override
    public TWarehouseArea selectTWarehouseAreaById(Long fId) {
        return tWarehouseAreaMapper.selectTWarehouseAreaById(fId);
    }

    /**
     * 查询库区列表
     *
     * @param tWarehouseArea 库区
     * @return 库区
     */
    @Override
    public List<TWarehouseArea> selectTWarehouseAreaList(TWarehouseArea tWarehouseArea) {
        return tWarehouseAreaMapper.selectTWarehouseAreaList(tWarehouseArea);
    }

    /**
     * 新增库区
     *
     * @param tWarehouseArea 库区
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTWarehouseArea(TWarehouseArea tWarehouseArea) {
        tWarehouseArea.setCreateTime(DateUtils.getNowDate());
        tWarehouseAreaMapper.insertTWarehouseArea(tWarehouseArea);
        // 修改仓库库容
        int i = updateWarehousefTotalgross(tWarehouseArea.getfWarehouseid());
        if (i <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }

    /**
     * 修改库区
     *
     * @param tWarehouseArea 库区
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTWarehouseArea(TWarehouseArea tWarehouseArea) {
        tWarehouseArea.setUpdateTime(DateUtils.getNowDate());
        tWarehouseAreaMapper.updateTWarehouseArea(tWarehouseArea);
        // 修改仓库库容
        int i = updateWarehousefTotalgross(tWarehouseArea.getfWarehouseid());
        if (i <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;

    }

    /**
     * 批量删除库区
     *
     * @param fId 需要删除的库区ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteTWarehouseAreaByIds(Long[] fId) {
        for(Long id:fId){
            TWarehousebillsitems tWarehousebillsitems =new TWarehousebillsitems();
            tWarehousebillsitems.setfWarehouselocid(id);
            int warehousebillsitemsSize = tWarehousebillsitemsMapper.warehousebillsitemsWarehouselocidSize(id);
            if(warehousebillsitemsSize > 0 ){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("库区有货物不可删除");
            }
            tWarehouseAreaMapper.deleteTWarehouseAreaById(id);
        }
        return AjaxResult.success();
    }

    /**
     * 删除库区信息
     *
     * @param fId 库区ID
     * @return 结果
     */
    @Override
    public int deleteTWarehouseAreaById(Long fId) {
        return tWarehouseAreaMapper.deleteTWarehouseAreaById(fId);
    }

    @Override
    public String checkUFTWarehouseUnique(TWarehouseArea tWarehouseArea) {
        TWarehouse tWarehouse = tWarehouseMapper.selectTWarehousById(tWarehouseArea.getfWarehouseid());
        if (StringUtils.isNotNull(tWarehouse) ) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     *  修改仓库库容
     * @param fWarehouseid
     * @return
     */
    public  int updateWarehousefTotalgross(Long fWarehouseid){
        // 查询仓库
        TWarehouse tWarehouse =tWarehouseMapper.selectTWarehouseById(fWarehouseid);
        String newfWarehouseid =tWarehouse.getAncestors().substring(tWarehouse.getAncestors().lastIndexOf(",")+1);
        // 所有库容
        BigDecimal fTotalgross = tWarehouseAreaMapper.selectTWarehouseArea(fWarehouseid);
        // 修改仓库扩容
        TWarehouse tWarehouseNew = new TWarehouse();
        tWarehouseNew.setfId(Long.valueOf(newfWarehouseid));
        tWarehouseNew.setfTotalgross(fTotalgross);
        return  tWarehouseMapper.updatefTotalgross(tWarehouseNew);
    }
}
