package com.mortals.xhx.module.attendance.web;
import cn.hutool.core.stream.CollectorUtil;
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.*;
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 java.util.List;
import java.util.Map;
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 DeptService deptService;
    @Autowired
    private AttendanceGroupFixedworkService attendanceGroupFixedworkService;
    @Autowired
    private AttendanceGroupFixedworkSpecialService specialService;
    @Autowired
    private AttendanceGroupResponsibleService groupResponsibleService;

    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 {
        entity.getAttendanceGroupFixedworkEntity().setGroupId(entity.getId());
        entity.getAttendanceGroupFixedworkEntity().setGroupName(entity.getGroupName());
        AttendanceGroupFixedworkEntity save = attendanceGroupFixedworkService.save(entity.getAttendanceGroupFixedworkEntity());
        List<AttendanceGroupFixedworkSpecialEntity> fixedworkSpecial = entity.getFixedworkSpecial();
        if (fixedworkSpecial!=null && fixedworkSpecial.size()>0){
        fixedworkSpecial.forEach(item->{
            item.setFixedWorkId(save.getId());
        });
        }
        specialService.save(fixedworkSpecial);
        AttendanceGroupResponsibleEntity responsibleEntity = entity.getGroupResponsibleEntity();
        responsibleEntity.setGroupId(entity.getId());
        responsibleEntity.setGroupName(entity.getGroupName());
        groupResponsibleService.save(responsibleEntity);
        return super.saveAfter(entity, model, context);
    }
}
