package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.MusicArrearageStudentDto;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderAddStudentDto;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.page.ArrearageStudentsQueryInfo;
import com.ym.mec.biz.dal.page.MusicCalenderDetailQueryInfo;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderDetailService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RequestMapping("musicGroupPaymentCalenderDetail")
@Api(tags = "乐团缴费明细")
@RestController
public class MusicGroupPaymentCalenderDetailController extends BaseController {

    @Autowired
    private MusicGroupPaymentCalenderDetailService musicGroupPaymentCalenderDetailService;
    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "分页查询乐团缴费明细列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/queryPage')")
    public Object queryPage(MusicCalenderDetailQueryInfo queryInfo) {
        return succeed(musicGroupPaymentCalenderDetailService.queryDetailPage(queryInfo));
    }

    @ApiOperation(value = "修改学员预计缴费金额")
    @PostMapping("/updateExpectAmount")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/updateExpectAmount')")
    public Object updateExpectAmount(BigDecimal expectAmount,BigDecimal expectMemberAmount,String ids) {
//        musicGroupPaymentCalenderDetailService.updateExpectAmount(expectAmount,expectMemberAmount,ids);
        return succeed();
    }

    @ApiOperation(value = "新增缴费学员列表获取")
    @PostMapping("/queryFeeStudents")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/queryFeeStudents')")
    public Object queryFeeStudents(String musicGroupId,String search,Integer subjectId) {
        return succeed(musicGroupPaymentCalenderDetailService.queryFeeStudents(musicGroupId,search,subjectId));
    }

    @ApiOperation(value = "乐团缴费记录新增学员")
    @PostMapping("/batchAdd")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/batchAdd')")
	public Object batchAdd(@RequestBody MusicGroupPaymentCalenderAddStudentDto musicGroupPaymentCalenderAddStudentDto) {
		musicGroupPaymentCalenderDetailService.batchAdd(musicGroupPaymentCalenderAddStudentDto.getBatchNo(),
				musicGroupPaymentCalenderAddStudentDto.getUserIdList());
		return succeed();
	}

    @ApiOperation(value = "批量删除缴费信息")
    @PostMapping("/batchDel")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/batchDel')")
    public Object batchDel(String musicGroupPaymentCalenderDetailIds) {
    	if(StringUtils.isBlank(musicGroupPaymentCalenderDetailIds)){
    		return failed("请求参数错误");
    	}
        musicGroupPaymentCalenderDetailService.batchDel(musicGroupPaymentCalenderDetailIds);
        return succeed();
    }

    @ApiOperation(value = "开启缴费")
    @PostMapping("/openPayment")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/openPayment')")
    public HttpResponseResult openPayment(String ids, Date startPaymentDate, Date deadlinePaymentDate) {
        HttpResponseResult<Object> succeed = succeed();
        Date date = new Date();
        startPaymentDate = startPaymentDate == null ? date : startPaymentDate;
		deadlinePaymentDate = deadlinePaymentDate == null ? date : deadlinePaymentDate;
        succeed.setMsg(musicGroupPaymentCalenderDetailService.openPayment(ids, startPaymentDate, deadlinePaymentDate));
        return succeed;
    }

    @ApiOperation(value = "发送学员待续费通知")
    @PostMapping("/sendWaitRenewMessage")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/sendWaitRenewMessage')")
    public HttpResponseResult sendWaitRenewMessage(Long calenderId, String studentIds) {
        musicGroupPaymentCalenderDetailService.pushWaitRenewMessage(calenderId,studentIds);
        return succeed();
    }

    @ApiOperation(value = "未缴费学员列表")
    @GetMapping("/queryArrearageStudents")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/queryArrearageStudents')")
    public HttpResponseResult<PageInfo<MusicArrearageStudentDto>> queryArrearageStudents(ArrearageStudentsQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(musicGroupPaymentCalenderDetailService.queryArrearageStudents(queryInfo));
    }

    @ApiOperation(value = "获取未缴费学员关联乐团列表")
    @GetMapping("/getNoPaymentStudentMusicGroups")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalenderDetail/getNoPaymentStudentMusicGroups')")
    public HttpResponseResult<List<MusicGroup>> getNoPaymentStudentMusicGroups(String organIds){
        organIds = organizationService.getEmployeeOrgan(organIds);
        return succeed(musicGroupPaymentCalenderDetailService.getNoPaymentStudentMusicGroups(organIds));
    }
}
