package com.ruoyi.web.modules.goodsservice.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.goodsservice.constant.GoodsServiceConstants;
import com.ruoyi.web.modules.goodsservice.domain.BizGoodsService;
import com.ruoyi.web.modules.goodsservice.exception.GoodsServiceBatchDeleteException;
import com.ruoyi.web.modules.goodsservice.facade.IGoodsSnapshotFacade;
import com.ruoyi.web.modules.goodsservice.mapper.BizGoodsServiceMapper;
import com.ruoyi.web.modules.goodsservice.service.IGoodsServiceCatalogService;
import com.ruoyi.web.modules.goodsservice.vo.GoodsServiceListVO;

/**
 * 平台商品服务目录实现
 */
@Service
public class GoodsServiceCatalogServiceImpl implements IGoodsServiceCatalogService
{
    private static final String DICT_YES_NO = "sys_yes_no";

    @Autowired
    private BizGoodsServiceMapper goodsServiceMapper;

    @Autowired
    private IGoodsSnapshotFacade goodsSnapshotFacade;

    @Override
    public List<GoodsServiceListVO> selectGoodsServiceList(BizGoodsService query)
    {
        List<BizGoodsService> list = goodsServiceMapper.selectList(query);
        List<GoodsServiceListVO> rows = new ArrayList<>();
        for (BizGoodsService row : list)
        {
            rows.add(toListVo(row));
        }
        return rows;
    }

    @Override
    public BizGoodsService selectGoodsServiceById(Long serviceId)
    {
        BizGoodsService row = goodsServiceMapper.selectById(serviceId);
        if (row == null)
        {
            throw new ServiceException("商品服务不存在或已删除");
        }
        return row;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGoodsService(BizGoodsService row)
    {
        validateFields(row, true);
        if (goodsServiceMapper.countByName(row.getServiceName(), null) > 0)
        {
            throw new ServiceException("服务名称已存在");
        }
        row.setDelFlag(GoodsServiceConstants.DEL_FLAG_NORMAL);
        return goodsServiceMapper.insert(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateGoodsService(BizGoodsService row)
    {
        if (row.getServiceId() == null)
        {
            throw new ServiceException("服务ID不能为空");
        }
        BizGoodsService existing = goodsServiceMapper.selectById(row.getServiceId());
        if (existing == null)
        {
            throw new ServiceException("商品服务不存在或已删除");
        }
        validateFields(row, false);
        if (goodsServiceMapper.countByName(row.getServiceName(), row.getServiceId()) > 0)
        {
            throw new ServiceException("服务名称已存在");
        }
        return goodsServiceMapper.update(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGoodsServiceByIds(Long[] serviceIds, String operator)
    {
        if (serviceIds == null || serviceIds.length == 0)
        {
            throw new ServiceException("请选择要删除的服务");
        }
        List<String> reasons = new ArrayList<>();
        List<Long> idList = new ArrayList<>();
        for (Long serviceId : serviceIds)
        {
            BizGoodsService row = goodsServiceMapper.selectById(serviceId);
            if (row == null)
            {
                throw new ServiceException("商品服务不存在或已删除");
            }
            if (goodsSnapshotFacade.existsGoodsRef(serviceId))
            {
                reasons.add("服务「" + row.getServiceName() + "」已被商品使用，无法删除");
            }
            else
            {
                idList.add(serviceId);
            }
        }
        if (!reasons.isEmpty())
        {
            throw new GoodsServiceBatchDeleteException("整批操作已取消", reasons);
        }
        goodsServiceMapper.logicDeleteByIds(idList, operator);
    }

    void validateFields(BizGoodsService row, boolean insert)
    {
        if (StringUtils.isEmpty(row.getDefaultShowFlag()))
        {
            row.setDefaultShowFlag(GoodsServiceConstants.DEFAULT_SHOW_YES);
        }
        if (row.getSortNo() == null)
        {
            row.setSortNo(0);
        }
        if (StringUtils.isEmpty(row.getServiceName()))
        {
            throw new ServiceException("请输入服务名称");
        }
        if (row.getServiceName().length() > 64)
        {
            throw new ServiceException("服务名称长度不能超过64个字符");
        }
        if (StringUtils.isEmpty(row.getServiceIntro()))
        {
            row.setServiceIntro("");
        }
        else if (row.getServiceIntro().length() > 500)
        {
            throw new ServiceException("简介长度不能超过500个字符");
        }
        if (StringUtils.isEmpty(row.getServiceIcon()))
        {
            row.setServiceIcon("");
        }
        else if (row.getServiceIcon().length() > 512)
        {
            throw new ServiceException("图标地址长度不能超过512个字符");
        }
        if (row.getSortNo() < 0)
        {
            throw new ServiceException("排序须为非负整数");
        }
        validateDefaultShowFlag(row.getDefaultShowFlag());
    }

    private void validateDefaultShowFlag(String flag)
    {
        if (!GoodsServiceConstants.DEFAULT_SHOW_YES.equals(flag)
                && !GoodsServiceConstants.DEFAULT_SHOW_NO.equals(flag))
        {
            throw new ServiceException("默认显示参数无效");
        }
    }

    private GoodsServiceListVO toListVo(BizGoodsService row)
    {
        GoodsServiceListVO vo = new GoodsServiceListVO();
        vo.setServiceId(row.getServiceId());
        vo.setServiceName(row.getServiceName());
        vo.setServiceIntro(row.getServiceIntro());
        vo.setServiceIcon(row.getServiceIcon());
        vo.setDefaultShowFlag(row.getDefaultShowFlag());
        vo.setSortNo(row.getSortNo());
        String label = DictUtils.getDictLabel(DICT_YES_NO, row.getDefaultShowFlag());
        if (StringUtils.isEmpty(label))
        {
            label = GoodsServiceConstants.DEFAULT_SHOW_YES.equals(row.getDefaultShowFlag()) ? "是" : "否";
        }
        vo.setDefaultShowFlagLabel(label);
        return vo;
    }
}
