package com.mortals.xhx.module.workman.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.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.WorkmanEntity;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*
* 工作人员
*
* @author zxfei
* @date 2023-04-08
*/
@RestController
@RequestMapping("workman")
public class WorkmanController extends BaseCRUDJsonBodyMappingController<WorkmanService,WorkmanEntity,Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private DeptService deptService;


    public WorkmanController(){
        super.setModuleDesc( "工作人员");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        //this.addDict(model, "positionId",deptService .find(new DeptQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getDeptName(),(o, n)->n)));
        List<DeptEntity> list = deptService.find(new DeptQuery());
        List<DeptTreeSelect> treeSelects = deptService.buildDeptTreeSelect(list);
        this.addDict(model, "deptId",treeSelects);
        super.init(model, context);
    }


    @Override
    protected int saveAfter(WorkmanEntity entity, Map<String, Object> model, Context context) throws AppException {
        DeptEntity deptEntity = new DeptEntity();
        deptEntity.setWorkName(entity.getName());
        return super.saveAfter(entity, model, context);
    }

    @Override
    protected void saveBefore(WorkmanEntity entity, Map<String, Object> model, Context context) throws AppException {
        DeptEntity deptEntity = deptService.selectOne(new DeptQuery().id(entity.getDeptId()));
        entity.setDeptName(deptEntity.getDeptName());
        super.saveBefore(entity, model, context);
    }
}
