package com.keao.edu.user.controller;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.tenant.OrganContextHolder;
import com.keao.edu.common.tenant.TenantContextHolder;
import com.keao.edu.user.dto.ExamManualLedgerStatisticsDto;
import com.keao.edu.user.entity.ExamManualLedger;
import com.keao.edu.user.enums.TransDirectionEnum;
import com.keao.edu.user.page.ExamManualLedgerQueryInfo;
import com.keao.edu.user.service.ExamManualLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Joburgess
 * @Date 2020.07.07
 */
@RestController
@RequestMapping("examManualLedger")
@Api(tags = "支出记录服务")
public class ExamManualLedgerController extends BaseController {

    @Autowired
    private ExamManualLedgerService examManualLedgerService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation("分页查询")
    @GetMapping(value = "/list")
    public HttpResponseResult<PageInfo<ExamManualLedger>> getList(ExamManualLedgerQueryInfo queryInfo) {
        return succeed(examManualLedgerService.queryExamManualLedgers(queryInfo));
    }

    @ApiOperation("新增支出记录")
    @PostMapping(value = "/add")
    public HttpResponseResult add(@RequestBody ExamManualLedger examManualLedger) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examManualLedger.setTransDirection(TransDirectionEnum.EXPENDITURE);
        examManualLedger.setTenantId(TenantContextHolder.getTenantId());
        examManualLedger.setOperatorUserId(sysUser.getId());
        examManualLedger.setOrganId(OrganContextHolder.getOrganId());
        examManualLedgerService.addExamManualLedger(examManualLedger);
        return succeed();
    }

    @ApiOperation("更新支出记录")
    @PostMapping(value = "/update")
    public HttpResponseResult update(@RequestBody ExamManualLedger examManualLedger) {
        examManualLedgerService.update(examManualLedger);
        return succeed();
    }

    @ApiOperation("删除支出记录")
    @PostMapping(value = "/del")
    public HttpResponseResult del(Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examManualLedgerService.deleteExamManualLedger(sysUser.getId(),id);
        return succeed();
    }

    @ApiOperation("获取考级项目支出统计信息")
    @GetMapping(value = "/getExamManualLedgerStatistics")
    public HttpResponseResult<ExamManualLedgerStatisticsDto> getExamManualLedgerStatistics(Long examId, Integer targetOrganId){
        return succeed(examManualLedgerService.getExamManualLedgerStatistics(OrganContextHolder.getOrganId(),examId,targetOrganId));
    }

}
