package com.mortals.xhx.module.attendance.web;
import cn.hutool.core.stream.CollectorUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.feign.IFeign;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.model.DeptTreeSelect;
import com.mortals.xhx.module.dept.service.DeptService;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.model.Context;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
*
* 考勤组信息
*
* @author zxfei
* @date 2023-04-08
*/
@RestController
@RequestMapping("attendance/group")
public class AttendanceGroupController extends BaseCRUDJsonBodyMappingController<AttendanceGroupService,AttendanceGroupEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private WorkmanService workmanService;
    @Autowired
    private AttendanceClassService attendanceClassService;
    @Autowired
    private AttendanceClassDetailService attendanceClassDetailService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private AttendanceGroupFixedworkService attendanceGroupFixedworkService;
    @Autowired
    private AttendanceGroupFixedworkSpecialService specialService;
    @Autowired
    private AttendanceGroupResponsibleService groupResponsibleService;
    @Autowired
    private AttendanceGroupFreeworkService freeworkService;
    @Autowired
    private AttendanceGroupService attendanceGroupService;

    public AttendanceGroupController(){
        super.setModuleDesc( "考勤组信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "type", paramService.getParamBySecondOrganize("AttendanceGroup","type"));
        this.addDict(model, "attendanceType", paramService.getParamBySecondOrganize("AttendanceGroup","attendanceType"));
        List<DeptEntity> list = deptService.find(new DeptQuery());
        List<DeptTreeSelect> treeSelects = deptService.buildDeptTreeSelect(list);
        this.addDict(model,"deptId",treeSelects);
        List<AttendanceClassEntity> attendanceClassEntities = attendanceClassService.find(new AttendanceClassQuery());
        this.addDict(model,"classId",attendanceClassEntities);
        this.addDict(model,"workMan",workmanService.find(new WorkmanQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getName(),(o, n)->n)));
        super.init(model, context);
    }

    @Override
    protected int saveAfter(AttendanceGroupEntity entity, Map<String, Object> model, Context context) throws AppException {
        AttendanceGroupFixedworkQuery condition = new AttendanceGroupFixedworkQuery();
        condition.setGroupId(entity.getId());
        attendanceGroupFixedworkService.getDao().delete(condition);

        AttendanceGroupResponsibleQuery responsibleQuery = new AttendanceGroupResponsibleQuery();
        responsibleQuery.setGroupId(entity.getId());
        groupResponsibleService.getDao().delete(responsibleQuery);

        AttendanceGroupFreeworkQuery freeworkQuery = new AttendanceGroupFreeworkQuery();
        freeworkQuery.setGroupId(entity.getId());
        freeworkService.getDao().delete(freeworkQuery);

    if (entity.getGroupFixedworkEntity()!=null){
            entity.getGroupFixedworkEntity().setGroupId(entity.getId());
            entity.getGroupFixedworkEntity().setGroupName(entity.getGroupName());
            entity.getGroupFixedworkEntity().setCreateUserId(this.getContextUserId(context));
            entity.getGroupFixedworkEntity().setCreateTime(new Date());
            attendanceGroupFixedworkService.save(entity.getGroupFixedworkEntity(),context);
        }

        AttendanceGroupResponsibleEntity responsibleEntity = entity.getGroupResponsibleEntity();
        responsibleEntity.setGroupId(entity.getId());
        responsibleEntity.setGroupName(entity.getGroupName());
        responsibleEntity.setCreateUserId(this.getContextUserId(context));
        responsibleEntity.setCreateTime(new Date());
        groupResponsibleService.save(responsibleEntity,context);

        if (entity.getAttendanceGroupFreeworkEntity()!=null){
            entity.getAttendanceGroupFreeworkEntity().setGroupId(entity.getId());
            entity.getAttendanceGroupFreeworkEntity().setGroupName(entity.getGroupName());
            entity.getAttendanceGroupFreeworkEntity().setCreateUserId(this.getContextUserId(context));
            entity.getAttendanceGroupFreeworkEntity().setCreateTime(new Date());
            freeworkService.save(entity.getAttendanceGroupFreeworkEntity(),context);
        }
        return super.saveAfter(entity, model, context);
    }

    @Override
    protected int editAfter(Long id, Map<String, Object> model, AttendanceGroupEntity entity, Context context) throws AppException {
        AttendanceGroupResponsibleEntity attendanceGroupResponsibleEntity = groupResponsibleService.selectOne(new AttendanceGroupResponsibleQuery().groupId(id));

        AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(new AttendanceGroupFixedworkQuery().groupId(id));

        AttendanceGroupFreeworkEntity attendanceGroupFreeworkEntity = freeworkService.selectOne(new AttendanceGroupFreeworkQuery().groupId(id));
        entity.setGroupFixedworkEntity(attendanceGroupFixedworkEntity);
        entity.setAttendanceGroupFreeworkEntity(attendanceGroupFreeworkEntity);
        entity.setGroupResponsibleEntity(attendanceGroupResponsibleEntity);
        return super.editAfter(id, model, entity, context);
    }

    @Override
    protected int doListAfter(AttendanceGroupEntity query, Map<String, Object> model, Context context) throws AppException {
        List<AttendanceGroupEntity> data = (List<AttendanceGroupEntity>)model.get(KEY_RESULT_DATA);
        for (AttendanceGroupEntity entity : data) {
            entity.setPersonNum(entity.getAttendanceGroupStaffList().size());
            AttendanceGroupResponsibleEntity attendanceGroupResponsibleEntity = groupResponsibleService.selectOne(new AttendanceGroupResponsibleQuery().groupId(entity.getId()));
            if (attendanceGroupResponsibleEntity!=null){
                entity.setResponsibleId(attendanceGroupResponsibleEntity.getResponsibleId());
                entity.setResponsibleName(attendanceGroupResponsibleEntity.getResponsibleName());
            }
            String monday = "";
            String tuesday = "";
            String wednesday = "";
            String thursday = "";
            String friday = "";
            String saturday = "";
            String sunday = "";
            String rest = "";
            String attendanceTime = "";
            //拼接考勤时间
            //固定班制
            AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(new AttendanceGroupFixedworkQuery().groupId(entity.getId()));
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            if (Objects.nonNull(attendanceGroupFixedworkEntity)){
                //周一
                if (Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getMonday()==1){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getMondayClassId()));
                    String className = attendanceGroupFixedworkEntity.getMondayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    monday ="周一"+"("+className+":"+time+")";
                    attendanceTime = attendanceTime+monday;
                }else {
                    rest = rest+"周一";
                }
                //周二
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getTuesday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getTuesdayClassId()));
                    String className = attendanceGroupFixedworkEntity.getTuesdayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    tuesday = "周二"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+tuesday;
                    }else {
                        attendanceTime = attendanceTime +tuesday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周二";
                    }else {
                        rest = rest+"周二";
                    }
                }
                //周三
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getWednesday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getWednesdayClassId()));
                    String className = attendanceGroupFixedworkEntity.getWednesdayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    wednesday = "周三"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+wednesday;
                    }else {
                        attendanceTime = attendanceTime +wednesday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周三";
                    }else {
                        rest = rest+"周三";
                    }
                }
                //周四
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getThursday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getThursdayClassId()));
                    String className = attendanceGroupFixedworkEntity.getThursdayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    thursday = "周四"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+thursday;
                    }else {
                        attendanceTime = attendanceTime +thursday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周四";
                    }else {
                        rest = rest+"周四";
                    }
                }
                //周五
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getFriday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getFridayClassId()));
                    String className = attendanceGroupFixedworkEntity.getFridayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    friday = "周五"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+friday;
                    }else {
                        attendanceTime = attendanceTime +friday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周五";
                    }else {
                        rest = rest+"周五";
                    }
                }
                //周六
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getSaturday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getSaturdayClassId()));
                    String className = attendanceGroupFixedworkEntity.getSaturdayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    saturday = "周六"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+saturday;
                    }else {
                        attendanceTime = attendanceTime +saturday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周六";
                    }else {
                        rest = rest+"周六";
                    }
                }
                //周日
                if ((Objects.nonNull(attendanceGroupFixedworkEntity)&&attendanceGroupFixedworkEntity.getSunday()==1)){
                    List<AttendanceClassDetailEntity> attendanceClassDetailEntity = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceGroupFixedworkEntity.getSundayClassId()));
                    String className = attendanceGroupFixedworkEntity.getSundayClassName();
                    String time = "";
                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntity) {
                        if (!time.equals("")){
                            time = time+"-"+sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }else {
                            time = sdf.format(classDetailEntity.getGoWorkDate())+"~"+sdf.format(classDetailEntity.getOffWorkDate());
                        }
                    }
                    sunday = "周日"+"("+className+":"+time+")";
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime + "、"+sunday;
                    }else {
                        attendanceTime = attendanceTime +sunday;
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周日";
                    }else {
                        rest = rest+"周日";
                    }
                }
            }

            //自由工时
            AttendanceGroupFreeworkEntity attendanceGroupFreeworkEntity = freeworkService.selectOne(new AttendanceGroupFreeworkQuery().groupId(entity.getId()));
            if (Objects.nonNull(attendanceGroupFreeworkEntity)){
                if (attendanceGroupFreeworkEntity.getMonday()==1){
                    attendanceTime="周一";
                }else {
                    rest = "周一";
                }
                if (attendanceGroupFreeworkEntity.getTuesday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周二";
                    }else {
                        attendanceTime = "周二";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周二";
                    }else {
                        rest = rest+"周二";
                    }
                }
                if (attendanceGroupFreeworkEntity.getWednesday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周三";
                    }else {
                        attendanceTime = "周三";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周三";
                    }else {
                        rest = rest+"周三";
                    }
                }
                if (attendanceGroupFreeworkEntity.getThursday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周四";
                    }else {
                        attendanceTime = "周四";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周四";
                    }else {
                        rest = rest+"周四";
                    }
                }
                if (attendanceGroupFreeworkEntity.getFriday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周五";
                    }else {
                        attendanceTime = "周五";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周五";
                    }else {
                        rest = rest+"周五";
                    }
                }
                if (attendanceGroupFreeworkEntity.getSaturday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周六";
                    }else {
                        attendanceTime = "周六";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周六";
                    }else {
                        rest = rest+"周六";
                    }
                }
                if (attendanceGroupFreeworkEntity.getSunday()==1){
                    if (!attendanceTime.equals("")){
                        attendanceTime = attendanceTime +"、"+"周日";
                    }else {
                        attendanceTime = "周日";
                    }
                }else {
                    if (!rest.equals("")){
                        rest = rest+"、"+"周日";
                    }else {
                        rest = rest+"周日";
                    }
                }
                if (!attendanceTime.equals("")){
                    attendanceTime = attendanceTime +"(工作时长:"+attendanceGroupFreeworkEntity.getWorkTime()+")";
                }
            }

            if (!rest.equals("")){
                rest = rest + "休息";
            }
            entity.setRestTime(rest);
            entity.setAttendanceTime(attendanceTime);
        }
        return super.doListAfter(query, model, context);
    }
}
