package com.ruoyi.mall.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.Vo.BatchDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallBatchDetailMapper;
import com.ruoyi.mall.domain.MallBatchDetail;
import com.ruoyi.mall.service.IMallBatchDetailService;

import javax.annotation.Resource;

/**
 * 批次说明Service业务层处理
 *
 * @author lsm
 * @date 2023-10-19
 */
@Service
public class MallBatchDetailServiceImpl implements IMallBatchDetailService
{
    @Resource
    private MallBatchDetailMapper mallBatchDetailMapper;

    /**
     * 查询批次说明
     *
     * @param batchDetailId 批次说明主键
     * @return 批次说明
     */
    @Override
    public MallBatchDetail selectMallBatchDetailByBatchDetailId(Long batchDetailId)
    {
        return mallBatchDetailMapper.selectMallBatchDetailByBatchDetailId(batchDetailId);
    }

    /**
     * 查询批次说明列表
     *
     * @param mallBatchDetail 批次说明
     * @return 批次说明
     */
    @Override
    public List<MallBatchDetail> selectMallBatchDetailList(MallBatchDetail mallBatchDetail)
    {
        return mallBatchDetailMapper.selectMallBatchDetailList(mallBatchDetail);
    }

    /**
     * 新增批次说明
     *
     * @param mallBatchDetail 批次说明
     * @return 结果
     */
    @Override
    public int insertMallBatchDetail(MallBatchDetail mallBatchDetail)
    {
        mallBatchDetail.setCreateAt(DateUtils.getTime());
        return mallBatchDetailMapper.insertMallBatchDetail(mallBatchDetail);
    }

    /**
     * 修改批次说明
     *
     * @param mallBatchDetail 批次说明
     * @return 结果
     */
    @Override
    public int updateMallBatchDetail(MallBatchDetail mallBatchDetail)
    {
        mallBatchDetail.setUpdateAt(DateUtils.getTime());
        return mallBatchDetailMapper.updateMallBatchDetail(mallBatchDetail);
    }

    /**
     * 批量删除批次说明
     *
     * @param batchDetailIds 需要删除的批次说明主键
     * @return 结果
     */
    @Override
    public int deleteMallBatchDetailByBatchDetailIds(Long[] batchDetailIds)
    {
        return mallBatchDetailMapper.deleteMallBatchDetailByBatchDetailIds(batchDetailIds);
    }

    /**
     * 删除批次说明信息
     *
     * @param batchDetailId 批次说明主键
     * @return 结果
     */
    @Override
    public int deleteMallBatchDetailByBatchDetailId(Long batchDetailId)
    {
        return mallBatchDetailMapper.deleteMallBatchDetailByBatchDetailId(batchDetailId);
    }

    /**
     * 批次说明
     * @return 结果
     */
    @Override
    public AjaxResult selectBatchDetailList() {
        Long provinceId = 1532L;
        List<BatchDetailVO> detailList = mallBatchDetailMapper.selectBatchDetailList(provinceId);

        int batchNum = 0;
        List<String> batchNameList = new ArrayList<>();

        if (detailList.size() > 0){
            batchNum = detailList.size();

            for (BatchDetailVO batchDetailVO : detailList) {
                if(batchDetailVO.getDescription().length()>50){
                    batchDetailVO.setShortDescription(batchDetailVO.getDescription().substring(0,50) + "...");
                }else{
                    batchDetailVO.setShortDescription(batchDetailVO.getDescription());
                }
                batchNameList.add(batchDetailVO.getBatchName());
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("batchNum", batchNum);
        result.put("batchNameList", batchNameList);
        result.put("detailList" , detailList);
        return AjaxResult.success(result);
    }
}
