package com.mortals.xhx.module.attendance.web;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
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 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/record/error")
public class AttendanceRecordErrorController extends BaseCRUDJsonBodyMappingController<AttendanceRecordErrorService,AttendanceRecordErrorEntity,Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private AttendanceGroupService attendanceGroupService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private AttendanceClassService attendanceClassService;

    @Autowired
    private AttendanceRecordService attendanceRecordService;

    @Autowired
    private AttendanceRecordDetailService attendanceRecordDetailService;

    @Autowired
    private AttendanceRecordErrorService errorService;

    public AttendanceRecordErrorController(){
        super.setModuleDesc( "考勤打卡记录异常信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "errorStatus", paramService.getParamBySecondOrganize("AttendanceRecordError","errorStatus"));
        this.addDict(model, "processStatus", paramService.getParamBySecondOrganize("AttendanceRecordError","processStatus"));
        this.addDict(model,"deptId",deptService.find(new DeptQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getDeptName())));
        this.addDict(model,"groupId",attendanceGroupService.find(new AttendanceGroupQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getGroupName())));
        this.addDict(model,"classId",attendanceClassService.find(new AttendanceClassQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getClassName())));
        super.init(model, context);
    }


    @PostMapping({"disposeError"})
    public Rest<String> disposeError(@RequestBody AttendanceRecordErrorEntity query) {
        Rest<String> ret = new Rest();
        Context context = this.getContext();
        query.setOpertor(context.getUser().getRealName());
        query.setOperDateTime(new Date());
        query.setProcessStatus(1);
        errorService.update(query);
        DateUtils.convertTime2Str(query.getErrorDateTime().getTime(),DateUtils.P_yyyy_MM_dd);
        AttendanceRecordQuery attendanceRecordQuery = new AttendanceRecordQuery();
        attendanceRecordQuery.setStaffId(query.getStaffId());
        attendanceRecordQuery.setAttendanceDateStart(DateUtils.convertTime2Str(query.getErrorDateTime().getTime(),DateUtils.P_yyyy_MM_dd));
        attendanceRecordQuery.setAttendanceDateEnd(DateUtils.convertTime2Str(query.getErrorDateTime().getTime(),DateUtils.P_yyyy_MM_dd));
        AttendanceRecordEntity attendanceRecordEntity = attendanceRecordService.selectOne(attendanceRecordQuery);
        AttendanceRecordDetailQuery detailQuery = new AttendanceRecordDetailQuery();
        detailQuery.setRecordId(attendanceRecordEntity.getId());
        detailQuery.setShiftsId(query.getShiftsId());
        List<AttendanceRecordDetailEntity> attendanceRecordDetailEntity = attendanceRecordDetailService.find(detailQuery);
        for (AttendanceRecordDetailEntity recordDetailEntity : attendanceRecordDetailEntity) {
            Date goWorkDate = recordDetailEntity.getGoWorkDate();
            Date offWorkDate = recordDetailEntity.getOffWorkDate();
            if (goWorkDate.getTime()==query.getErrorDateTime().getTime()){
                recordDetailEntity.setGoWorkResult(query.getErrorStatus());
            }
            if (offWorkDate.getTime()==query.getErrorDateTime().getTime()){
                recordDetailEntity.setOffWorkResult(query.getErrorStatus());
            }
            attendanceRecordDetailService.update(recordDetailEntity);
        }
        return  Rest.ok();
    }




}
