package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.ClassGroupStudentMapperDao;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.ClassGroupStudentStatusEnum;
import com.ym.mec.biz.dal.page.MusicGroupPaymentCalenderQueryInfo;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderCourseSettingsService;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;

@RequestMapping("musicGroupPaymentCalender")
@Api(tags = "乐团缴费日历服务")
@RestController
public class MusicGroupPaymentCalenderController extends BaseController {

    @Autowired
    private MusicGroupPaymentCalenderService musicGroupPaymentCalenderService;
    @Autowired
    private MusicGroupPaymentCalenderCourseSettingsService musicGroupPaymentCalenderCourseSettingsService;
	@Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private ClassGroupStudentMapperDao classGroupStudentMapperDao;

    @ApiOperation(value = "分页查询乐团缴费日历列表")
    @GetMapping(value = "/queryPage", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/queryPage')")
    public Object queryPage(MusicGroupPaymentCalenderQueryInfo queryInfo) {
        return succeed(musicGroupPaymentCalenderService.queryPage(queryInfo));
    }

    @ApiOperation(value = "获取缴费信息")
    @GetMapping("/getDetail")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/getDetail')")
    public Object getDetail(Long id) {
        return succeed(musicGroupPaymentCalenderService.getDetail(id));
    }

    @ApiOperation(value = "新增乐团缴费日历")
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/add')")
    public Object add(@RequestBody MusicGroupPaymentCalenderDto musicGroupPaymentCalenderDto) {
    	String batchNo = musicGroupPaymentCalenderService.create(musicGroupPaymentCalenderDto);
        ModelMap map = new ModelMap();
        map.put("musicGroupPaymentCalenderBatchNo", batchNo);
        return succeed(map);
    }

    @ApiOperation(value = "删除乐团缴费日历")
    @PostMapping("/del")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/del')")
    public Object del(Long id) {
        musicGroupPaymentCalenderService.del(id);
        return succeed();
    }

    @ApiOperation(value = "删除乐团缴费日历")
    @PostMapping("/delByBatchNo")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/delByBatchNo')")
    public Object delByBatchNo(String batchNo) {
        musicGroupPaymentCalenderService.deleteByBatchNo(batchNo);
        return succeed();
    }

    @ApiOperation(value = "修改乐团缴费时间")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/update')")
    public Object update(@RequestBody MusicGroupPaymentCalenderDto musicGroupPaymentCalenderDto) {
    	String batchNo = musicGroupPaymentCalenderService.merge(musicGroupPaymentCalenderDto);
        ModelMap map = new ModelMap();
        map.put("musicGroupPaymentCalenderBatchNo", batchNo);
        return succeed();
    }

    @ApiOperation(value = "确认学校已缴费")
    @PostMapping("/makesureSchoolePaid")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/makesureSchoolePaid')")
	public Object makesureSchoolePaid(Long id, String memo) {
		musicGroupPaymentCalenderService.makesureSchoolePaid(id, memo);
		return succeed();
	}

    @ApiOperation(value = "乐团缴费日历审核列表")
    @GetMapping("/auditList")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/auditList')")
    public Object auditList(MusicGroupPaymentCalenderQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(musicGroupPaymentCalenderService.auditList(queryInfo));
    }

    @ApiOperation(value = "乐团缴费日历审核列表详情")
    @GetMapping("/auditListDetail")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/auditListDetail')")
    public Object auditListDetail(String batchNo) {
        return succeed(musicGroupPaymentCalenderService.auditListDetail(batchNo));
    }

    @ApiOperation(value = "乐团缴费日历审核通过")
    @PostMapping("/auditPass")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/auditPass')")
    public Object auditPass(String batchNo,String auditMemo) {
        musicGroupPaymentCalenderService.auditPass(batchNo,auditMemo);
        return succeed();
    }

    @ApiOperation(value = "乐团缴费日历审核拒绝")
    @PostMapping("/auditRefuse")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/auditRefuse')")
    public Object auditRefuse(String batchNo,String auditMemo) {
        musicGroupPaymentCalenderService.auditRefuse(batchNo,auditMemo);
        return succeed();
    }

    @ApiOperation(value = "获取指定学员在指定乐团下本次课排课时长")
    @GetMapping("/getMusicCourseSettingsWithStudents")
    @PreAuthorize("@pcs.hasPermissions('musicGroupPaymentCalender/getMusicCourseSettingsWithStudents')")
    public HttpResponseResult<Map<String, Integer>> getMusicCourseSettingsWithStudents(String musicGroupId, String studentIds, Long classGroupId){
        List<Integer> studentIdList = new ArrayList<>();
        if(StringUtils.isNotBlank(studentIds)){
            studentIdList = Arrays.stream(studentIds.split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
        }
        if(Objects.nonNull(classGroupId)){
            List<StudentRegistration> studentList = classGroupStudentMapperDao.findClassStudentList(classGroupId.intValue(), ClassGroupStudentStatusEnum.NORMAL);
            studentIdList = studentList.stream().map(StudentRegistration::getUserId).collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(studentIdList)){
            throw new BizException("未指定学员");
        }
        return succeed(musicGroupPaymentCalenderCourseSettingsService.getMusicCourseSettingsWithStudents(musicGroupId,studentIdList));
    }
}
