package io.renren.modules.app.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.R;
import io.renren.modules.admin.entity.SignEntity;
import io.renren.modules.admin.service.SignService;
import io.renren.modules.app.service.UserService;



/**
 * 签到表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-14 15:21:05
 */
@RestController
@RequestMapping("app/sign")
public class Sign {
    @Autowired
    private SignService signService;
    @Autowired
    private UserService userService;
    /**
     * 保存
     */
    @PostMapping("/signin")
    public R save(@RequestParam Map<String, Object> params){
        Long attendersId = Long.valueOf(params.get("attendersId").toString());
        SignEntity sEntity =  signService.getOne(
            new QueryWrapper<SignEntity>().eq("attenders_id", attendersId)
        );
        if (sEntity == null) {
            Map<String, Object> orderInfo = userService.queryOrderInfoByAttendersId(attendersId);
            if (Integer.valueOf(orderInfo.get("isCheck").toString()) == 0) {
                return R.error(1, "签到未成功");
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date dateStart = simpleDateFormat.parse(orderInfo.get("startTime").toString());
                long startTime = dateStart.getTime();
                Date dateEnd = simpleDateFormat.parse(orderInfo.get("endTime").toString());
                long endTime = dateEnd.getTime();
                long nowtime = System.currentTimeMillis();
                if (nowtime < startTime || nowtime > endTime) {
                    return R.error(2, "签到未成功");
                }
            } catch (Exception e) {
                return R.error();
            }
            SignEntity signEntity = new SignEntity();
            signEntity.setAttendersId(attendersId);
            signEntity.setMeetingId(Long.valueOf(orderInfo.get("meetingId").toString()));
            signEntity.setStatus(1);
            signEntity.setBadge(0);
            signEntity.setCreateTime(new Date());
            signEntity.setLastTime(signEntity.getCreateTime());
            signEntity.setFlag(1L);
            signEntity.setIsDel(0);
            signService.save(signEntity);
            return R.ok().put("signInfo", signEntity);
        } else {
            sEntity.setLastTime(new Date());
            sEntity.setFlag(sEntity.getFlag() + 1L);
            signService.updateById(sEntity);
            return R.ok().put("signInfo", sEntity);
        }
    }
}
