package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CourseScheduleTeacherSalaryDao;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.TeacherAttendancesDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.enums.TeacherSalaryConfirmStatus;
import com.ym.mec.biz.dal.page.CourseSalaryQueryInfo4Web;
import com.ym.mec.biz.dal.page.CourseScheduleTeacherSalaryQueryInfo;
import com.ym.mec.biz.service.CourseScheduleTeacherSalaryService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("courseScheduleTeacherSalary")
@Api(tags = "教师薪酬服务")
@RestController
public class CourseScheduleTeacherSalaryController extends BaseController {

    @Autowired
    private CourseScheduleTeacherSalaryService courseScheduleTeacherSalaryService;
    @Autowired
    private CourseScheduleTeacherSalaryDao courseScheduleTeacherSalaryDao;
    @Autowired
    private VipGroupService vipGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation(value = "分页查询教师薪酬列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('courseScheduleTeacherSalary/queryPage')")
    public Object queryPage(CourseScheduleTeacherSalaryQueryInfo queryInfo) {
        return succeed(courseScheduleTeacherSalaryService.querySalaries(queryInfo));
    }

    @ApiOperation(value = "获取已结算的课酬记录")
    @GetMapping("/findIsSettlementCourseSalarys")
    @PreAuthorize("@pcs.hasPermissions('courseScheduleTeacherSalary/findIsSettlementCourseSalarys')")
    public HttpResponseResult findIsSettlementCourseSalarys(CourseSalaryQueryInfo4Web queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败");
        }
        if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeDao.get(sysUser.getId());
            if (org.apache.commons.lang3.StringUtils.isEmpty(queryInfo.getOrganIdList())) {
                queryInfo.setOrganIdList(employee.getOrganIdList());
            } else if (org.apache.commons.lang3.StringUtils.isEmpty(employee.getOrganIdList())) {
                throw new BizException("用户所在分部异常");
            } else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if (!list.containsAll(Arrays.asList(queryInfo.getOrganIdList().split(",")))) {
                    throw new BizException("非法请求");
                }
            }
        }

        Map<String, Object> result=new HashMap<>();
        result.put("pageInfo", courseScheduleTeacherSalaryService.findIsSettlementCourseSalarys(queryInfo));
        int i = courseScheduleTeacherSalaryDao.countOpenConfirmSalarysWithMonth(queryInfo.getMonth(), TeacherSalaryConfirmStatus.UNCONFIRMED);
        if(i>0){
            result.put("isOpenConfirm", 1);
        }else{
            int j = courseScheduleTeacherSalaryDao.countOpenConfirmSalarysWithMonth(queryInfo.getMonth(), null);
            if(j<=0){
                result.put("isOpenConfirm", 2);
            }else{
                result.put("isOpenConfirm", 0);
            }
        }
        return succeed(result);
    }

    @ApiOperation(value = "开启课酬确认")
    @GetMapping("/openSalaryConfirm")
    @PreAuthorize("@pcs.hasPermissions('courseScheduleTeacherSalary/openSalaryConfirm')")
    public HttpResponseResult openSalaryConfirm(String month){
        courseScheduleTeacherSalaryService.openSalaryConfirm(month);
        return succeed();
    }

    @ApiOperation(value = "关闭课酬确认")
    @GetMapping("/closeSalaryConfirm")
    @PreAuthorize("@pcs.hasPermissions('courseScheduleTeacherSalary/closeSalaryConfirm')")
    public HttpResponseResult closeSalaryConfirm(String month){
        courseScheduleTeacherSalaryService.closeSalaryConfirm(month);
        return succeed();
    }

    @ApiOperation(value = "获取指定课程上的教师信息")
    @GetMapping("/getCourseTeachers")
    @PreAuthorize("@pcs.hasPermissions('courseScheduleTeacherSalary/getCourseTeachers')")
    public HttpResponseResult<List<TeacherAttendancesDto>> getCourseTeachers(Long courseScheduleId){
        if(Objects.isNull(courseScheduleId)){
            throw new BizException("请指定课程");
        }
        return succeed(courseScheduleTeacherSalaryService.getCourseTeachers(courseScheduleId));
    }
}
