package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityDto;
import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatDto;
import com.ym.mec.biz.dal.dto.ReplacementPayDto;
import com.ym.mec.biz.dal.entity.ReplacementInstrumentActivity;
import com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation;
import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
import com.ym.mec.biz.dal.enums.PayStatus;
import com.ym.mec.biz.service.ReplacementInstrumentActivityService;
import com.ym.mec.biz.service.ReplacementInstrumentCooperationService;
import com.ym.mec.biz.service.StudentPaymentOrderService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("replacementInstrumentActivity")
@Api(tags = "乐器置换调查问卷")
@RestController
public class ReplacementInstrumentActivityController extends BaseController {

    @Autowired
    private ReplacementInstrumentActivityService replacementInstrumentActivityService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ReplacementInstrumentCooperationService replacementInstrumentCooperationService;
    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;

    @ApiOperation(value = "新增调查问卷")
    @PostMapping("/insert")
    public Object add(ReplacementInstrumentActivity replacementInstrumentActivity) {
        return succeed(replacementInstrumentActivityService.add(replacementInstrumentActivity));
    }

    @ApiOperation(value = "新版新增调查问卷")
    @PostMapping("/apply")
    public Object apply(@RequestBody ReplacementInstrumentActivityDto replacementInstrumentActivityDto) {
        return succeed(replacementInstrumentActivityService.apply(replacementInstrumentActivityDto));
    }

    @ApiOperation(value = "新版获取学员基本信息")
    @GetMapping("queryUserInfoByCooperationId")
    public Object queryUserInfoByCooperationId(Integer cooperationId) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        ReplacementInstrumentCooperation replacementInstrumentCooperation = replacementInstrumentCooperationService.get(cooperationId);
        Integer cooperationOrganId = replacementInstrumentCooperation.getCooperationOrganId();
        return succeed(replacementInstrumentActivityService.queryUserInfo(cooperationOrganId,user,cooperationId));
    }

    @ApiOperation(value = "获取学员基本信息")
    @GetMapping("queryUserInfo")
    public Object queryUserInfo(Integer cooperationOrganId) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        ReplacementInstrumentCooperation cooperation = replacementInstrumentCooperationService.findByCooperationOrganIdAndTopicId(cooperationOrganId,null);
        return succeed(replacementInstrumentActivityService.queryUserInfo(cooperationOrganId,user,cooperation.getId()));
    }

    @ApiOperation(value = "修改调查问卷")
    @PostMapping("/update")
    public Object update(ReplacementInstrumentActivity replacementInstrumentActivity) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败，请重新登陆");
        }
        replacementInstrumentActivityService.update(replacementInstrumentActivity);
        return succeed();
    }

    @ApiOperation(value = "获取调查问卷")
    @GetMapping("/get")
    public Object get(Integer id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败，请重新登陆");
        }
        return succeed(replacementInstrumentActivityService.get(id));
    }

    @ApiOperation(value = "获取置换的详情")
    @GetMapping("/getInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "置换id", required = true, dataType = "int")})
    public HttpResponseResult<ReplacementInstrumentActivityStatDto> getInfo(Integer id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败，请重新登陆");
        }
        return succeed(replacementInstrumentActivityService.getInfo(id));
    }

    @ApiOperation(value = "获取置换的详情")
    @GetMapping("/getInfoByCooperationOrganId")
    @ApiImplicitParams({@ApiImplicitParam(name = "cooperationOrganId", value = "合作单位id", required = true, dataType = "int")})
    public HttpResponseResult<ReplacementInstrumentActivityStatDto> getInfoByCooperationOrganId(Integer cooperationOrganId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请重新登陆");
        }
        ReplacementInstrumentActivity replacementInstrumentActivity = replacementInstrumentActivityService.findByUserId(cooperationOrganId, sysUser.getId(),null);
        if(replacementInstrumentActivity == null){
        	return failed("未查询到您参与调查的记录");
        }
        Integer activityId = replacementInstrumentActivity.getId();
        return succeed(replacementInstrumentActivityService.getInfo(activityId));
    }

    @ApiOperation(value = "新版获取置换的详情")
    @GetMapping("/getInfoByCooperationId")
    @ApiImplicitParams({@ApiImplicitParam(name = "cooperationId", value = "置换活动编号", required = true, dataType = "int")})
    public HttpResponseResult<ReplacementInstrumentActivityStatDto> getInfoByCooperationId(Integer cooperationId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请重新登陆");
        }
        ReplacementInstrumentCooperation replacementInstrumentCooperation = replacementInstrumentCooperationService.get(cooperationId);
        ReplacementInstrumentActivity replacementInstrumentActivity = replacementInstrumentActivityService.findByUserId(replacementInstrumentCooperation.getCooperationOrganId(),
                sysUser.getId(),
                replacementInstrumentCooperation.getId());
        if(replacementInstrumentActivity == null){
        	return failed("未查询到您参与调查的记录");
        }
        Integer activityId = replacementInstrumentActivity.getId();
        return succeed(replacementInstrumentActivityService.getInfo(activityId));
    }

    @ApiOperation(value = "置换支付")
    @PostMapping("/pay")
    public HttpResponseResult pay(@RequestBody ReplacementPayDto replacementPayDto) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败，请重新登陆");
        }
        ReplacementInstrumentActivity replacementInstrumentActivity = replacementInstrumentActivityService.get(replacementPayDto.getId());
        if (replacementInstrumentActivity == null) {
            throw new BizException("乐器置换不存在，请核查");
        }
        if (!sysUser.getId().equals(replacementInstrumentActivity.getUserId())) {
            throw new BizException("该乐器置换不属于您，请核查");
        }

        if (replacementInstrumentActivity.getInstrumentsId() == null || replacementInstrumentActivity.getInstrumentsId() == 0) {
            throw new BizException("没有置换的乐器，请核查");
        }

        //是否已支付
        if (replacementInstrumentActivity.getPayStatus().equals(2)) {
            return failed("您已支付成功，请勿重复支付");
        }
        //判断用户是否已存在订单
        if (replacementInstrumentActivity.getPayStatus().equals(1) && !replacementPayDto.getRePay()) {
        	
        	StudentPaymentOrder applyOrder = studentPaymentOrderService.getUserReplacementIngOrder(replacementInstrumentActivity.getUserId(), replacementInstrumentActivity.getId());
        	// 查询订单状态
            PayStatus payStatus = studentPaymentOrderService.queryPayStatus(applyOrder);
            if(payStatus == PayStatus.SUCCESSED){
        		throw new BizException("订单已支付成功，请勿重复支付");
        	}/*else if(payStatus == PayStatus.PAYING){
        		throw new BizException("订单还在交易中，请稍后重试");
        	}*/
            return failed(HttpStatus.CONTINUE, "您有待支付的订单");
        }
        Map payMap = replacementInstrumentActivityService.pay(replacementPayDto);
        if (payMap.containsKey("tradeState")) {
            return failed(HttpStatus.CREATED, payMap, "恭喜您,支付成功!");
        }
        return succeed(payMap);
    }
}
