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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.SubListForPageUtil;
import io.hmit.entity.UserEntity;
import io.hmit.modules.serviceOrder.dao.EnrollDao;
import io.hmit.modules.serviceOrder.dto.EnrollDTO;
import io.hmit.modules.serviceOrder.dto.PensionEventDTO;
import io.hmit.modules.serviceOrder.entity.EnrollEntity;
import io.hmit.modules.serviceOrder.entity.PensionEventEntity;
import io.hmit.modules.serviceOrder.service.EnrollService;
import io.hmit.modules.serviceOrder.service.PensionEventService;
import io.hmit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

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

/**
 * 报名表
 *
 * @author Shen Yuanfeng syf0412@vip.qq.com
 * @since 1.0.0 2021-05-12
 */
@Slf4j
@Service
public class EnrollServiceImpl extends CrudServiceImpl<EnrollDao, EnrollEntity, EnrollDTO> implements EnrollService {

    @Autowired
    private PensionEventService pensionEventService;

    @Autowired
    private UserService userService;

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

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

        return wrapper;
    }


    @Override
    public Boolean addEnrollInfo(EnrollDTO enrollDTO, Long eventId) {
        try {
            PensionEventEntity pensionEventEntity = pensionEventService.selectById(eventId);
            BeanUtils.copyProperties(pensionEventEntity, enrollDTO);
            enrollDTO.setEventId(eventId);
            UserEntity userEntity = userService.selectById(enrollDTO.getUserId());
            enrollDTO.setName(userEntity.getUsername());
            enrollDTO.setMobile(userEntity.getMobile());
            Integer gender = 1;
            if (null != userEntity.getGender() && userEntity.getGender().equals("女")) {
                gender = 2;
            }
            enrollDTO.setId(null);
            enrollDTO.setStatus(0);
            enrollDTO.setCreateDate(null);
            enrollDTO.setUpdateDate(null);
            enrollDTO.setGender(gender);
            save(enrollDTO);
            //TODO 推送用户报名信息
        }catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

}