package com.fs.store.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsVessel;
import com.fs.store.mapper.FsVesselMapper;
import com.fs.store.param.FsVesselListUParam;
import com.fs.store.service.IFsVesselService;
import com.fs.store.vo.FsVesselListUVO;
import com.fs.store.vo.FsVesselListVO;
import com.fs.store.vo.OptionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 经络管理Service业务层处理
 *
 * @author fs
 * @date 2023-10-23
 */
@Service
public class FsVesselServiceImpl implements IFsVesselService
{
    @Autowired
    private FsVesselMapper fsVesselMapper;

    /**
     * 查询经络管理
     *
     * @param id 经络管理主键
     * @return 经络管理
     */
    @Override
    public FsVessel selectFsVesselById(Long id)
    {
        return fsVesselMapper.selectFsVesselById(id);
    }

    /**
     * 查询经络管理列表
     *
     * @param fsVessel 经络管理
     * @return 经络管理
     */
    @Override
    public List<FsVessel> selectFsVesselList(FsVessel fsVessel)
    {
        return fsVesselMapper.selectFsVesselList(fsVessel);
    }

    /**
     * 新增经络管理
     *
     * @param fsVessel 经络管理
     * @return 结果
     */
    @Override
    public int insertFsVessel(FsVessel fsVessel)
    {
        fsVessel.setCreateTime(DateUtils.getNowDate());
        return fsVesselMapper.insertFsVessel(fsVessel);
    }

    /**
     * 修改经络管理
     *
     * @param fsVessel 经络管理
     * @return 结果
     */
    @Override
    public int updateFsVessel(FsVessel fsVessel)
    {
        return fsVesselMapper.updateFsVessel(fsVessel);
    }

    /**
     * 批量删除经络管理
     *
     * @param ids 需要删除的经络管理主键
     * @return 结果
     */
    @Override
    public int deleteFsVesselByIds(Long[] ids)
    {
        return fsVesselMapper.deleteFsVesselByIds(ids);
    }

    /**
     * 删除经络管理信息
     *
     * @param id 经络管理主键
     * @return 结果
     */
    @Override
    public int deleteFsVesselById(Long id)
    {
        return fsVesselMapper.deleteFsVesselById(id);
    }

    @Override
    public List<FsVesselListVO> selectFsVesselListVO(FsVessel fsVessel) {
        return fsVesselMapper.selectFsVesselListVO(fsVessel);
    }

    @Override
    public String importData(List<FsVessel> list) {
        if (com.fs.common.utils.StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FsVessel vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsVesselMapper.insertFsVessel(vo);

                successNum++;
                successMsg.append("<br/>" + successNum + "、名称 " + vo.getVesselName() + " 导入成功");

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + vo.getVesselName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<OptionsVO> selectFsVesselListOptions() {
        return fsVesselMapper.selectFsVesselListOptions();
    }

    @Override
    public List<FsVesselListUVO> selectFsVesselListUVO(FsVesselListUParam param) {
        return fsVesselMapper.selectFsVesselListUVO(param);
    }
}
