package com.mortals.xhx.module.attendance.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.attendance.model.AttendanceClassDetailEntity;
import com.mortals.xhx.module.workman.model.WorkmanEntity;
import com.mortals.xhx.module.workman.model.WorkmanQuery;
import com.mortals.xhx.module.workman.service.WorkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.attendance.model.AttendanceClassEntity;
import com.mortals.xhx.module.attendance.service.AttendanceClassService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 考勤班次信息
 *
 * @author zxfei
 * @date 2023-04-08
 */
@RestController
@RequestMapping("attendance/class")
public class AttendanceClassController extends BaseCRUDJsonBodyMappingController<AttendanceClassService, AttendanceClassEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private WorkmanService workmanService;

    public AttendanceClassController() {
        super.setModuleDesc("考勤班次信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "workManId", workmanService.find(new WorkmanQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getName(), (o, n) -> n)));
        super.init(model, context);
    }

    @Override
    protected void saveBefore(AttendanceClassEntity entity, Map<String, Object> model, Context context) throws AppException {

        if (!ObjectUtils.isEmpty(entity.getClassResponsiblePersonId())) {
            WorkmanEntity workmanEntity = workmanService.get(entity.getClassResponsiblePersonId());
            entity.setClassResponsiblePersonName(workmanEntity == null ? "" : workmanEntity.getName());
        }
        super.saveBefore(entity, model, context);
    }
}
