package com.ym.mec.web.controller.student;
import com.ym.mec.biz.dal.entity.StudentRecharge;
import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.page.StudentCashDetailQueryInfo;
import com.ym.mec.biz.service.StudentRechargeService;
import com.ym.mec.biz.service.SysUserCashAccountDetailService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @version V1.0
 * @Description: TODO
 * @date Date : 2019年09月24日 10:00
 */
@RequestMapping("cashAccountDetail")
@Api(tags = "学生充值")
@RestController
public class StudentCashAccountDetailController extends BaseController {


    @Autowired
    private SysUserCashAccountDetailService sysUserCashAccountDetailService;

    @Autowired
    private StudentRechargeService studentRechargeService;
    @ApiOperation(value = "账户流水列表")
    @GetMapping(value = "/queryPage",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object queryPage(@RequestBody(required = false) StudentCashDetailQueryInfo queryInfo){
        if(queryInfo == null){
            queryInfo = new StudentCashDetailQueryInfo();
        }
        return succeed(sysUserCashAccountDetailService.queryPage(queryInfo));
    }

    /**
     * 同步
     * @return
     */
    @ApiOperation(value = "账户流水同步")
    @GetMapping(value = "/synTransStatus",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object synTransStatus(@RequestBody String transNo){

        if(StringUtils.isEmpty(transNo)){
            return failed();
        }

        //
        StudentRecharge studentRecharge = studentRechargeService.selectByTransNo(transNo);
        if(studentRecharge == null){
            return failed("交易不存在");
        }
        SysUserCashAccountDetail sysUserCashAccountDetail = sysUserCashAccountDetailService.selectDetailByTransNo(transNo);
        if(sysUserCashAccountDetail == null){
            return failed("交易不存在");
        }

        //TODO 查询第三方

        //更新充值记录
        studentRecharge.setStatus(DealStatusEnum.ING);
        studentRecharge.setModifyTime(new Date());
        studentRechargeService.update(studentRecharge);

        //更新流水记录
        sysUserCashAccountDetail.setStatus(DealStatusEnum.ING);
        sysUserCashAccountDetail.setUpdateTime(new Date());
        sysUserCashAccountDetailService.update(sysUserCashAccountDetail);
        return succeed();
    }
}
