package com.mortals.xhx.module.job.web;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.JobTypeEnum;
import com.mortals.xhx.module.job.model.JobEntity;
import com.mortals.xhx.module.job.model.JobQuery;
import com.mortals.xhx.module.job.service.JobService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.impl.StaffServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 职位信息
 *
 * @author zxfei
 * @date 2023-04-07
 */
@RestController
@RequestMapping("job")
public class JobController extends BaseCRUDJsonBodyMappingController<JobService, JobEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private StaffServiceImpl staffService;


    public JobController() {
        super.setModuleDesc("职位信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "type", JobTypeEnum.getEnumMap());
        this.addDict(model, "groupId",service.queryGroupName(new JobQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getGroupName(),(o, n)->n)));
        super.init(model, context);
    }

    @Override
    protected void doListBefore(JobEntity query, Map<String, Object> model, Context context)  {
        query.setSize(-1);
        super.doListBefore(query, model, context);
    }


    @Override
    protected int infoAfter(Long id, Map<String, Object> model, JobEntity entity, Context context) throws AppException {
        List<StaffEntity> staffEntityList = this.staffService.find(new StaffQuery().positionId(id));
        entity.setStaffEntities(staffEntityList);
        return super.infoAfter(id, model, entity, context);
    }
}
