package com.liquidnet.service.platform.controller.kylin;


import com.liquidnet.common.cache.redis.util.RedisKylinUtil;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dto.vo.KylinTimePerformanceVo;
import com.liquidnet.service.kylin.dto.vo.PerformanceOrderStatisticalVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinOrderTicketVo;
import com.liquidnet.service.kylin.service.partner.IKylinPerformancesPartnerService;
import com.liquidnet.service.platform.utils.PerformanceVoTask;
import com.mongodb.BasicDBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

import static com.liquidnet.commons.lang.util.DateUtil.DTF_YMD_HMS;

/**
 * <p>
 * 演出 前端控制器
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-05-11
 */
@Api(tags = "前端-演出")
@RestController
@RequestMapping("performance")
@Slf4j
public class KylinPerformancesController {

    @Autowired
    private PerformanceVoTask performanceVoTask;
    @Autowired
    private IKylinPerformancesPartnerService performancesPartnerService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisKylinUtil redisKylinUtil;

    @PostMapping("/checkPerformanceTime")
    @ApiOperation("演出脚本")
    public ResponseDto<String> checkPerformanceTime(@RequestParam(value = "performancesId", required = false) String performancesId) {
        try {
            performanceVoTask.performanceVoStatus(performancesId);
            return ResponseDto.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDto.success("失败");
        }
    }

    @GetMapping(value = "line")
    @ApiOperation(value = "演出状态管理", position = 9)
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<Boolean> performanceLine() {
        try {
            List<KylinTimePerformanceVo> voList = mongoTemplate.findAll(KylinTimePerformanceVo.class, KylinTimePerformanceVo.class.getSimpleName());
            for (KylinTimePerformanceVo item : voList) {
                if (LocalDateTime.now().isAfter(LocalDateTime.parse(item.getTime(), DTF_YMD_HMS).plusSeconds(2))) {
                    if (item.getTaskType() == 1) {
                        performancesPartnerService.onLinePerformance(item.getPerformanceId());
                        log.info("[FINISH ON LINE TASK] performanceId = " + item.getPerformanceId() + " time = " + item.getTime());
                    } else if (item.getTaskType() == 2) {
                        performancesPartnerService.outLinePerformance(item.getPerformanceId());
                        log.info("[FINISH OUT LINE TASK] performanceId = " + item.getPerformanceId() + " time = " + item.getTime());
                    }
                    if (item.getTaskType() != 0) {
                        mongoTemplate.getCollection(KylinTimePerformanceVo.class.getSimpleName()).updateMany(
                                Query.query(Criteria.where("performanceId").is(item.getPerformanceId())).getQueryObject(),
                                Update.update("taskType", 0).getUpdateObject());
                    }
                }
            }
            return ResponseDto.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDto.success(false);
        }
    }

    @GetMapping(value = "subscribePush")
    @ApiOperation(value = "开票提醒定时推送")
    public ResponseDto<Boolean> performanceSubscribePush() {
        try {
            //加锁10分钟避免数据量大重复提醒
            boolean lock = redisKylinUtil.lock(KylinRedisConst.REDIS_KEY_KYLIN_PERFORMANCE_SUBSCRIBE_LOCK, 1, 60*10);
            if(lock){
                log.info("kylin:performance:subscribe:lock开票提醒定时推送我没有加锁！！！！！！！");
                performanceVoTask.performanceSubscribePush();
                //解锁
                redisKylinUtil.uLock(KylinRedisConst.REDIS_KEY_KYLIN_PERFORMANCE_SUBSCRIBE_LOCK);
                log.info("kylin:performance:subscribe:lock开票提醒定时推送我解锁了！！！！！！！");
            }
            return ResponseDto.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            //解锁
            redisKylinUtil.uLock(KylinRedisConst.REDIS_KEY_KYLIN_PERFORMANCE_SUBSCRIBE_LOCK);
            return ResponseDto.success(false);
        }
    }

}
