package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ctop.entity.ReleaseRecord;
import org.jeecg.modules.ctop.entity.ReleaseViewRecord;
import org.jeecg.modules.ctop.mapper.ReleaseViewRecordMapper;
import org.jeecg.modules.ctop.service.IReleaseRecordService;
import org.jeecg.modules.ctop.service.IReleaseViewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 发版记录信息
 * @author jeecg-boot
 * @date   2020-06-10
 * @version V1.0
 */
@Service
public class ReleaseViewRecordServiceImpl extends ServiceImpl<ReleaseViewRecordMapper, ReleaseViewRecord> implements IReleaseViewRecordService {
    @Autowired
    private IReleaseRecordService releaseRecordService;
    @Override
    public Map<String, Object> check(String userId) {
        Map<String,Object>result = new HashMap<>();
        ReleaseRecord releaseRecord = releaseRecordService.getLastRecord();
        if(null==releaseRecord){
            result.put("showPopup",false);
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
            return result;
        }
        ReleaseViewRecord releaseViewRecord = getRecordByParams(userId,releaseRecord.getId());
        if(null!=releaseViewRecord){
            result.put("showPopup",false);
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
            return result;
        }
        result.put("showPopup",true);
        result.put("data",releaseRecord);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> view(String userId, JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        Long versionId = data.getLong("versionId");
        if(null == versionId||versionId == 0){
            ResultMapUtils.setResultMap(result,StatusCode.COMMON_VERSION_NOT_EXIST);
            return result;
        }
        ReleaseViewRecord releaseViewRecord = new ReleaseViewRecord();
        releaseViewRecord.setUserId(userId);
        releaseViewRecord.setReleaseId(versionId);
        releaseViewRecord.setCreateTime(new Date());
        releaseViewRecord.setUpdateTime(new Date());
        this.save(releaseViewRecord);
        ResultMapUtils.setResultMap(result,StatusCode.COMMON_SUCCESS);
        return result;
    }

    public ReleaseViewRecord getRecordByParams(String userId,Long releaseId){
        QueryWrapper<ReleaseViewRecord> queryWrapper = new QueryWrapper<>();
        if(null!=userId&&!"".equals(userId.trim())){
            queryWrapper.eq("user_id",userId);
        }
        if(null!=releaseId&&releaseId!=0){
            queryWrapper.eq("release_id",releaseId);
        }
        queryWrapper.orderByDesc("id").last("limit 1");
        return this.getOne(queryWrapper);
    }
}
