package com.mortals.xhx.module.workman.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.role.model.RoleQuery;
import com.mortals.xhx.base.system.role.model.RoleUserEntity;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.module.model.model.ModelQuery;
import com.mortals.xhx.module.model.service.ModelService;
import com.mortals.xhx.module.workman.model.WorkmanEntity;
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.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 工作人员
 *
 * @author zxfei
 * @date 2022-01-20
 */
@RestController
@RequestMapping("workman")
public class WorkmanController extends BasePhpCRUDJsonMappingController<WorkmanService, WorkmanForm, WorkmanEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleUserService roleUserService;
    @Autowired
    private ModelService modelService;

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

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "politicalstatus", paramService.getParamBySecondOrganize("Workman", "politicalstatus"));
        this.addDict(model, "dangyuan", paramService.getParamBySecondOrganize("Workman", "dangyuan"));
        this.addDict(model, "starlevel", paramService.getParamBySecondOrganize("Workman", "starlevel"));
        this.addDict(model, "online", paramService.getParamBySecondOrganize("Workman", "online"));

        Map<String, String> userCollect = userService.find(new UserQuery(), getContext()).stream()
                .filter(f -> !this.service.find(new WorkmanQuery()).stream().map(WorkmanEntity::getSystemUserId).collect(Collectors.toSet()).contains(f.getId()))
                .collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getRealName(), (o, n) -> n));
        WorkmanEntity workmanEntity = (WorkmanEntity) model.get(KEY_RESULT_ENTITY);
        if (!ObjectUtils.isEmpty(workmanEntity) && !ObjectUtils.isEmpty(workmanEntity.getSystemUserId())) {
            UserEntity userEntity = userService.get(workmanEntity.getSystemUserId());
            if (!ObjectUtils.isEmpty(userEntity)) {
                userCollect.put(userEntity.getId().toString(), userEntity.getRealName());
            }
        }

        Map<String, String> roleCollect = roleService.find(new RoleQuery())
                .stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getName(), (o, n) -> n));

        this.addDict(model, "systemUserId", userCollect);
        this.addDict(model, "roleId", roleCollect);
        this.addDict(model, "modelIds", modelService.find(new ModelQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getModelName())));


        super.init(model, context);
    }


    @Override
    protected int editAfter(Long id, Map<String, Object> model, WorkmanEntity entity, Context context) throws AppException {
        buildWorkman(entity);
        return super.editAfter(id, model, entity, context);
    }

    @Override
    protected int viewAfter(Long id, Map<String, Object> model, WorkmanEntity entity, Context context) throws AppException {
        buildWorkman(entity);
        return super.viewAfter(id, model, entity, context);
    }

    private void buildWorkman(WorkmanEntity entity) {
        UserEntity userEntity = userService.get(entity.getSystemUserId());
        if (!ObjectUtils.isEmpty(userEntity)) {
            entity.setLoginName(userEntity.getLoginName());
            entity.setLoginPwd(userEntity.getLoginPwd());
            RoleUserQuery roleUserQuery = new RoleUserQuery();
            roleUserQuery.setUserId(userEntity.getId());
            RoleUserEntity roleUserEntity = roleUserService.selectOne(roleUserQuery);
            if (!ObjectUtils.isEmpty(roleUserEntity)) {
                entity.setRoleId(roleUserEntity.getRoleId());
            }
        }
    }
}