package com.miaxis.app.controller.gzpt;


import com.miaxis.common.annotation.Log;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.newgzpt.domain.GzptTrainRecord;
import com.miaxis.newgzpt.domain.GzptUserInfo;
import com.miaxis.newgzpt.dto.TrainCreditDTO;
import com.miaxis.newgzpt.dto.TrainFinishDTO;
import com.miaxis.newgzpt.dto.TrainFinishKmDTO;
import com.miaxis.newgzpt.service.IGzptTrainRecordService;
import com.miaxis.newgzpt.service.IGzptUserInfoService;
import com.miaxis.newgzpt.vo.TrainCreditVO;
import com.miaxis.newgzpt.vo.TrainFinishKmVO;
import com.miaxis.newgzpt.vo.TrainFinishVO;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-03-23
 */
@RestController
@Api(tags = "【小程序-学时管理】")
@RequestMapping(Constants.STUDENT_PREFIX+"/gzpt-train-record")
public class GzptTrainRecordController extends BaseController {

    @Resource
    IGzptTrainRecordService gzptTrainRecordService;

    @Autowired
    private IGzptUserInfoService gzptUserInfoService;


    /**
     * 新增学时
     */
    @Log(title = "新增学时", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增学时")
    public Response add(@RequestBody GzptTrainRecord gzptTrainRecord){

        UserInfo student =  SecurityUtils.getLoginUser().getStudent();
        GzptUserInfo gzptUserInfo = gzptUserInfoService.getInfoById(student.getGzptUserId());
        if (gzptUserInfo == null) {
            throw new CustomException("该用户未查到相关的学员信息");
        }
        gzptTrainRecord.setStudentId(gzptUserInfo.getOutId());
        gzptTrainRecordService.saveGzptTrainRecord(gzptTrainRecord);
        return Response.success();
    }


    @GetMapping("/getTrainFinishNew")
    @ApiOperation("查询学员学时明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<TrainFinishVO> getTrainFinishNew(){
        startPage();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        GzptUserInfo gzptUserInfo = gzptUserInfoService.getInfoById(student.getGzptUserId());
        if (gzptUserInfo == null){
            throw new CustomException("该学员计时账户不存在");
        }

        TrainFinishDTO trainFinishDTO = new TrainFinishDTO();
        trainFinishDTO.setDqbh(gzptUserInfo.getCity());
        trainFinishDTO.setStudentId(gzptUserInfo.getOutId());
        List<TrainFinishVO> list = gzptTrainRecordService.getTrainFinishNew(trainFinishDTO);
        return toResponsePageInfo(list);
    }


    @GetMapping("/getTrainFinishKm")
    @ApiOperation("查询已完成科目学时")
    public ResponsePageInfo<TrainFinishKmVO> getTrainFinishKm(){
        startPage();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        GzptUserInfo gzptUserInfo = gzptUserInfoService.getInfoById(student.getGzptUserId());
        if (gzptUserInfo == null){
            throw new CustomException("该学员计时账户不存在");
        }
        TrainFinishKmDTO trainFinishKmDTO = new TrainFinishKmDTO();
        trainFinishKmDTO.setDqbh(gzptUserInfo.getCity());
        trainFinishKmDTO.setStudentId(gzptUserInfo.getOutId());
        List<TrainFinishKmVO> list = gzptTrainRecordService.getTrainFinishKm(trainFinishKmDTO);
        return toResponsePageInfo(list);
    }

    @GetMapping("/getTrainSubjectCredit")
    @ApiOperation("查询额定学时")
    public ResponsePageInfo<TrainCreditVO> getTrainSubjectCredit(){
        startPage();
        UserInfo student = SecurityUtils.getLoginUser().getStudent();
        GzptUserInfo gzptUserInfo = gzptUserInfoService.getInfoById(student.getGzptUserId());
        if (gzptUserInfo == null){
            throw new CustomException("该学员计时账户不存在");
        }
        TrainCreditDTO trainCreditDTO = new TrainCreditDTO();
        trainCreditDTO.setDqbh(gzptUserInfo.getCity());
        trainCreditDTO.setCx(gzptUserInfo.getCartype());
        List<TrainCreditVO> list = gzptTrainRecordService.getTrainSubjectCredit(trainCreditDTO);
        return toResponsePageInfo(list);
    }

}

