package com.willalp.canteen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.canteen.domain.HsGiftType;
import com.willalp.canteen.mapper.HsGiftTypeMapper;
import com.willalp.canteen.service.IHsGiftTypeService;
import com.willalp.common.utils.DateUtils;
import com.willalp.event.domain.HsEvent;
import com.willalp.event.service.IHsEventService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 礼品类型Service业务层处理
 *
 * @author willalp
 * @date 2022-12-19
 */
@Service
public class HsGiftTypeServiceImpl extends ServiceImpl<HsGiftTypeMapper, HsGiftType> implements IHsGiftTypeService {

    @Override
    public boolean save(HsGiftType entity) {
        HsGiftType info = baseMapper.selectById(entity.getParentId());
        // 如果父节点不为正常状态，则不允许新增子节点
        if (info == null) {
            return super.save(entity);
//            throw new BaseException("礼品类型停用，不允许新增");
        }
        entity.setAncestors(info.getAncestors() + "," + entity.getParentId());
        return super.save(entity);
    }

    /**
     * 查询礼品类型列表
     *
     *
     * @param hsGiftType 礼品类型
     * @return 礼品类型
     */
    @Override
    public List<HsGiftType> selectHsGiftTypeList(HsGiftType hsGiftType) {
        return baseMapper.selectHsGiftTypeList(hsGiftType);
    }

    @Resource
    private IHsEventService eventService;

    @Override
    public List<HsGiftType> appSelectHsGiftType() {
        List<HsGiftType> hsGiftTypeDb = baseMapper.selectHsGiftTypeList(null);
        List<HsGiftType> giftTypes = new ArrayList<>();
        HsEvent hsEvent = new HsEvent();
        hsEvent.setEventStatus("3");
        List<HsEvent> hsEvents = eventService.selectHsEventList(hsEvent);
        HsGiftType eventToParentGiftType = new HsGiftType();
        eventToParentGiftType.setGiftTypeName("预定兑换");
        List<HsGiftType> eventToParentGiftTypeChildList = new ArrayList<>();
        for (HsEvent event : hsEvents) {
            HsGiftType eventToGiftType = new HsGiftType();
            eventToGiftType.setGiftTypeName(DateUtils.parseDateToStr("MM/dd", event.getEndTime()) + "前");
            eventToGiftType.setId(event.getId());
            eventToParentGiftTypeChildList.add(eventToGiftType);
        }
        eventToParentGiftType.setChildren(eventToParentGiftTypeChildList);
        giftTypes.add(eventToParentGiftType);
        if (0 != hsGiftTypeDb.size()) {
            for (HsGiftType hsGiftType : hsGiftTypeDb) {
                if ("0".equals(String.valueOf(hsGiftType.getParentId()))) {
                    List<HsGiftType> list = new ArrayList<>();
                    for (HsGiftType hsGiftType1 : hsGiftTypeDb) {
                        if (hsGiftType.getId().equals(String.valueOf(hsGiftType1.getParentId()))) {
                            list.add(hsGiftType1);
                        }
                    }
                    hsGiftType.setChildren(list);
                    giftTypes.add(hsGiftType);
                }
            }
            return giftTypes;
        }
        return null;
    }

}
