package io.hmit.modules.serviceOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.enums.EventEnum;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.common.utils.SubListForPageUtil;
import io.hmit.modules.serviceOrder.dao.PensionEventDao;
import io.hmit.modules.serviceOrder.dto.EnrollDTO;
import io.hmit.modules.serviceOrder.dto.PensionEventDTO;
import io.hmit.modules.serviceOrder.entity.PensionEventEntity;
import io.hmit.modules.serviceOrder.service.PensionEventService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 活动表
 *
 * @author Shen Yuanfeng syf0412@vip.qq.com
 * @since 1.0.0 2021-05-12
 */
@Service
public class PensionEventServiceImpl extends CrudServiceImpl<PensionEventDao, PensionEventEntity, PensionEventDTO> implements PensionEventService {

    @Autowired
    private PensionEventDao pensionEventDao;

    @Override
    public QueryWrapper<PensionEventEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PensionEventEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public PensionEventDTO updateStatus(Long id, Integer status, String endDesc, String eventClock) {
        PensionEventEntity pensionEventEntity = selectById(id);
        if (status.equals(EventEnum.SUCCESS.getCode())) { //开始报名 / 发布
            if (pensionEventEntity.getStatus().equals(EventEnum.NEW.getCode())){
                pensionEventEntity.setStatus(EventEnum.SUCCESS.getCode());
            }
        }else if (status.equals(EventEnum.OVER.getCode())) { //停止报名
            if (pensionEventEntity.getStatus().equals(EventEnum.SUCCESS.getCode())){
                pensionEventEntity.setStatus(EventEnum.OVER.getCode());
            }
        }else if (status.equals(EventEnum.COMPLETE.getCode())) { //活动结束
            if (pensionEventEntity.getStatus().equals(EventEnum.OVER.getCode())){
                pensionEventEntity.setStatus(EventEnum.COMPLETE.getCode());
                pensionEventEntity.setEventClock(eventClock);
                pensionEventEntity.setEventEndDescribe(endDesc);
            }else if (pensionEventEntity.getStatus().equals(EventEnum.HIDDEN.getCode())) {
                pensionEventEntity.setStatus(EventEnum.COMPLETE.getCode());
            }
        }else if (status.equals(EventEnum.HIDDEN.getCode())){ //活动隐藏
            if (pensionEventEntity.getStatus().equals(EventEnum.COMPLETE.getCode())){
                pensionEventEntity.setStatus(EventEnum.HIDDEN.getCode());
            }
        }
        update(ConvertUtils.sourceToTarget(pensionEventEntity, PensionEventDTO.class));
        return null;
    }

    @Override
    public PageData<PensionEventDTO> getOrgEvent(Map<String, Object> params) {
        List<PensionEventEntity> list = pensionEventDao.getOrgEvent(Long.parseLong(params.get("orgId").toString()));
        return getPageData(SubListForPageUtil.getSubList(params, list), list.size(), PensionEventDTO.class);
    }
}