package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.SubjectGoodsMapper;
import com.ym.mec.biz.dal.page.SubjectQueryInfo;
import com.ym.mec.biz.service.SubjectService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("subject")
@Api(tags = "科目服务")
@RestController
public class SubjectController extends BaseController {

    @Autowired
    private SubjectService subjectService;

    @ApiOperation(value = "修改、新增科目")
    @PostMapping("/upset")
    @PreAuthorize("@pcs.hasPermissions('subject/upset')")
    public Object update(@RequestBody Subject subject){
        subjectService.upSetSubject(subject);
        return succeed();
    }

    @ApiOperation(value = "根据科目编号查询科目")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('subject/get')")
    public Object get(@ApiParam(value = "科目编号", required = true) @PathVariable("id") Integer id){
        return succeed(subjectService.get(id));
    }

    @ApiOperation(value = "分页查询科目列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('subject/queryPage')")
    public Object queryPage(SubjectQueryInfo queryInfo){
        return succeed(subjectService.queryPage(queryInfo));
    }

    @ApiOperation(value = "分页查询科目树状列表")
    @GetMapping("/queryPageTree")
    @PreAuthorize("@pcs.hasPermissions('subject/queryPageTree')")
    public Object queryPageTree(SubjectQueryInfo queryInfo){
        return succeed(subjectService.queryPageTree(queryInfo));
    }

    @ApiOperation(value = "通过乐团编号查询乐团科目规划")
    @GetMapping("/querySubByMusicGroupId")
    @PreAuthorize("@pcs.hasPermissions('subject/querySubByMusicGroupId')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public Object findSubByMusicGroupId(String musicGroupId){
        return succeed(subjectService.findSubByMusicGroupId(musicGroupId));
    }

    @ApiOperation(value = "通过乐团收费类型，获取默认的声部列表")
    @GetMapping("/findDefaultSubByChargeTypeId")
    @PreAuthorize("@pcs.hasPermissions('subject/findDefaultSubByChargeTypeId')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "chargeTypeId", value = "收费类型编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "organId", value = "分部编号", required = true, dataType = "Integer")})
    public Object findDefaultSubByChargeTypeId(Integer chargeTypeId,Integer organId,Integer number){
        if(chargeTypeId == null || organId == null || number == null){
            throw new BizException("参数校验错误");
        }
        return succeed(subjectService.findDefaultSubByChargeTypeId(chargeTypeId,organId,number));
    }

    @ApiOperation(value = "新建乐团时，获取设置声部费用默认数据")
    @GetMapping("/setSubjectInfo")
    @PreAuthorize("@pcs.hasPermissions('subject/setSubjectInfo')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "chargeTypeId", value = "收费类型编号", required = true, dataType = "Integer")})
    public Object setSubjectInfo(Integer chargeTypeId){
        return succeed(subjectService.setSubjectInfo(chargeTypeId));
    }

    @ApiOperation(value = "修改、新增声部关联的商品列表")
    @PostMapping("/markGoods")
    @PreAuthorize("@pcs.hasPermissions('subject/markGoods')")
    public Object markGoods(@RequestBody List<SubjectGoodsMapper> subjectGoodsMappers){
        subjectService.markGoods(subjectGoodsMappers);
        return succeed();
    }

    @ApiOperation(value = "通过乐团编号获取声部列表以及声部报名、缴费、计划人数")
    @GetMapping("/findSubApplyDetail")
    @PreAuthorize("@pcs.hasPermissions('subject/findSubApplyDetail')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public Object findSubApplyDetail(String musicGroupId){
        return succeed(subjectService.findSubApplyDetail(musicGroupId));
    }

    @ApiOperation(value = "获取子集科目列表")
    @GetMapping("/findSubSubjects")
    @PreAuthorize("@pcs.hasPermissions('subject/findSubSubjects')")
    public Object findSubSubjects(Integer tenantId){
        return succeed(subjectService.findSubSubjects(tenantId));
    }
}
