package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.MusicGroupGoodsAndDiscountDto;
import com.ym.mec.biz.dal.dto.MusicGroupSubjectGoodsAndInfoDto;
import com.ym.mec.biz.dal.entity.ChargeTypeSubjectMapper;
import com.ym.mec.biz.dal.entity.Goods;
import com.ym.mec.biz.dal.entity.MusicGroupSubjectPlan;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.ym.mec.biz.service.MusicGroupSubjectPlanService;
import com.ym.mec.common.controller.BaseController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("musicGroupSubjectPlan")
@Api(tags = "乐团声部计划服务")
@RestController
public class MusicGroupSubjectPlanController extends BaseController {

    @Autowired
    private MusicGroupSubjectPlanService musicGroupSubjectPlanService;

    @ApiOperation(value = "根据id查询乐团声部招生情况")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/get')")
    public Object get(@ApiParam(value = "乐团编号", required = true) @PathVariable("id") int id) {
        return succeed(musicGroupSubjectPlanService.get(id));
    }

    @ApiOperation(value = "乐团声部分班情况")
    @GetMapping("/getMusicSubjectClass")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/getMusicSubjectClass')")
    public Object getMusicSubjectClass(@ApiParam(value = "乐团编号", required = true) String musicGroupId) {
        return succeed(musicGroupSubjectPlanService.getMusicSubjectClassPlan(musicGroupId));
    }

    @ApiOperation(value = "获取乐团声部规划列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/queryPage')")
    public Object queryPage(@ApiParam(value = "乐团编号", required = true) String musicGroupId) {
        return succeed(musicGroupSubjectPlanService.getMusicSubjectClassPlan(musicGroupId));
    }

    @ApiOperation(value = "修改乐团声部规划费用")
    @PostMapping("/batchUpdateFee")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/batchUpdateFee')")
    public Object batchUpdateFee(@RequestBody List<MusicGroupSubjectPlan> musicGroupSubjectPlans) {
        return succeed(musicGroupSubjectPlanService.batchUpdateFee(musicGroupSubjectPlans));
    }

    @ApiOperation(value = "根据科目编号和乐团编号获取乐团规划")
    @PostMapping("/findSubjectPlan")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/findSubjectPlan')")
    public Object findSubjectPlan(String musicGroupId,Integer subjectId) {
        if(StringUtils.isEmpty(musicGroupId) || subjectId == null){
            throw new BizException("参数校验失败");
        }
        return succeed(musicGroupSubjectPlanService.findSubjectPlan(musicGroupId,subjectId));
    }


    @ApiOperation(value = "获取乐团商品和折扣")
    @GetMapping("/getSubjectGoods")
    @PreAuthorize("@pcs.hasPermissions('musicGroupSubjectPlan/getSubjectGoods')")
    @ApiImplicitParams({ @ApiImplicitParam(name = "subjectId", value = "科目编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "INSTRUMENT 乐器, ACCESSORIES 教辅", required = true, dataType = "String"),
            @ApiImplicitParam(name = "chargeTypeId", value = "收费模式id", required = true, dataType = "Integer")
    })
    public HttpResponseResult getSubjectGoods(String subjectIds, String type, Integer chargeTypeId) {
        if(StringUtils.isBlank(subjectIds)){
            return succeed();
        }
        Map<String, List<MusicGroupGoodsAndDiscountDto>> result = new HashMap<>();
        for (String subjectIdStr : subjectIds.split(",")) {
            result.put(subjectIdStr, musicGroupSubjectPlanService.getSubjectGoods(Integer.valueOf(subjectIdStr), type, chargeTypeId));
        }
        return succeed(result);
    }

}
