package com.bonus.material.ma.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import com.bonus.common.biz.enums.TmTaskTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.basic.domain.BmFileInfo;
import com.bonus.material.basic.mapper.BmFileInfoMapper;
import com.bonus.system.api.RemoteFileService;
import org.springframework.stereotype.Service;
import com.bonus.material.ma.mapper.SupplierInfoMapper;
import com.bonus.material.ma.domain.SupplierInfo;
import com.bonus.material.ma.service.ISupplierInfoService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 物资厂家管理Service业务层处理
 * 
 * @author bonus
 */
@Service
public class SupplierInfoServiceImpl implements ISupplierInfoService {
    public static final long MODEL_ID = 16L;

    @Resource
    private SupplierInfoMapper supplierInfoMapper;

    @Resource
    private RemoteFileService remoteFileService;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    /**
     * 查询物资厂家管理
     * 
     * @param supplierId 物资厂家管理主键
     * @return 物资厂家管理
     */
    @Override
    public SupplierInfo selectSupplierInfoBySupplierId(Long supplierId) {
        SupplierInfo supplierInfo = supplierInfoMapper.selectSupplierInfoBySupplierId(supplierId);
        BmFileInfo bmFileInfo = new BmFileInfo();
        bmFileInfo.setModelId(supplierInfo.getSupplierId());
        bmFileInfo.setTaskType(TmTaskTypeEnum.TM_TASK_FACTORY_MANAGEMENT.getTaskTypeId());
        List<BmFileInfo> fileInfos = bmFileInfoMapper.selectBmFileInfoList(bmFileInfo);
        supplierInfo.setBmFileInfos(fileInfos);
        return supplierInfo;
    }

    /**
     * 查询物资厂家管理列表
     * 
     * @param supplierInfo 物资厂家管理
     * @return 物资厂家管理
     */
    @Override
    public List<SupplierInfo> selectSupplierInfoList(SupplierInfo supplierInfo) {
        return supplierInfoMapper.selectSupplierInfoList(supplierInfo);
    }

    /**
     * 新增物资厂家管理
     * 
     * @param supplierInfo 物资厂家管理
     * @return 结果
     */
    @Override
    public AjaxResult insertSupplierInfo(SupplierInfo supplierInfo) {
        try {
            supplierInfo.setCreateTime(DateUtils.getNowDate());
            int insertedSupplierInfoResult = supplierInfoMapper.insertSupplierInfo(supplierInfo);
            if (insertedSupplierInfoResult > 0) {
                if (CollectionUtils.isEmpty(supplierInfo.getBmFileInfos())) {
                    return AjaxResult.success("新增任务成功,无营业执照附件");
                }
                AtomicBoolean addFileInfoResult = new AtomicBoolean(false);
                supplierInfo.getBmFileInfos().forEach(bmFileInfo -> {
                    bmFileInfo.setModelId(supplierInfo.getSupplierId());
                    bmFileInfo.setCreateTime(DateUtils.getNowDate());
                    addFileInfoResult.set(bmFileInfoMapper.insertBmFileInfo(bmFileInfo) > 0);
                });
                return addFileInfoResult.get() ? AjaxResult.success("新增任务成功") : AjaxResult.error("新增任务失败,附件表插入0条");
            } else {
                return AjaxResult.error("新增任务失败,info表插入0条");
            }
        } catch (Exception e) {
            throw new ServiceException("数据库错误或供应商名称重复");
        }
    }

    /**
     * 修改物资厂家管理
     * 
     * @param supplierInfo 物资厂家管理
     * @return 结果
     */
    @Override
    public AjaxResult updateSupplierInfo(SupplierInfo supplierInfo) {
        supplierInfo.setUpdateTime(DateUtils.getNowDate());
        try {
            int updatedSupplierInfoResult = supplierInfoMapper.updateSupplierInfo(supplierInfo);
            if (updatedSupplierInfoResult > 0) {
                BmFileInfo bmFileInfoToDelete = new BmFileInfo();
                bmFileInfoToDelete.setModelId(supplierInfo.getSupplierId());
                bmFileInfoToDelete.setTaskType(TmTaskTypeEnum.TM_TASK_FACTORY_MANAGEMENT.getTaskTypeId());
                bmFileInfoMapper.deleteBmFileInfoByBizInfo(bmFileInfoToDelete);
                if (CollectionUtils.isEmpty(supplierInfo.getBmFileInfos())) {
                    return AjaxResult.success("修改任务成功,无营业执照附件");
                }
                AtomicBoolean addFileInfoResult = new AtomicBoolean(false);
                supplierInfo.getBmFileInfos().forEach(bmFileInfo -> {
                    bmFileInfo.setModelId(supplierInfo.getSupplierId());
                    bmFileInfo.setCreateTime(DateUtils.getNowDate());
                    addFileInfoResult.set(bmFileInfoMapper.insertBmFileInfo(bmFileInfo) > 0);
                });
                return addFileInfoResult.get() ? AjaxResult.success("修改任务成功") : AjaxResult.error("修改任务失败,附件表插入0条");
            } else {
                return AjaxResult.error("修改任务失败,info表插入0条");
            }
        } catch (Exception e) {
            throw new ServiceException("数据库错误或供应商名称重复");
        }
    }

    /**
     * 批量删除物资厂家管理
     * 
     * @param supplierIds 需要删除的物资厂家管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierInfoBySupplierIds(Long[] supplierIds) {
        BmFileInfo bmFileInfoToDelete;
        for (int i = 0; i < supplierIds.length; i++) {
            bmFileInfoToDelete = new BmFileInfo();
            bmFileInfoToDelete.setModelId(supplierIds[i]);
            bmFileInfoToDelete.setTaskType(TmTaskTypeEnum.TM_TASK_FACTORY_MANAGEMENT.getTaskTypeId());
            bmFileInfoMapper.deleteBmFileInfoByBizInfo(bmFileInfoToDelete);
        }
        return supplierInfoMapper.deleteSupplierInfoBySupplierIds(supplierIds);
    }

    /**
     * 删除物资厂家管理信息
     * 
     * @param supplierId 物资厂家管理主键
     * @return 结果
     */
    @Override
    public int deleteSupplierInfoBySupplierId(Long supplierId) {
        return supplierInfoMapper.deleteSupplierInfoBySupplierId(supplierId);
    }
}
