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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.module.perform.model.PerformRulesCategoryQuery;
import com.mortals.xhx.module.perform.model.PerformRulesEntity;
import com.mortals.xhx.module.perform.service.PerformRulesCategoryService;
import com.mortals.xhx.module.perform.service.PerformRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

import static java.util.stream.Collectors.toMap;


/**
 * 绩效规则信息
 *
 * @author zxfei
 * @date 2023-05-16
 */
@RestController
@RequestMapping("perform/rules")
public class PerformRulesController extends BaseCRUDJsonBodyMappingController<PerformRulesService, PerformRulesEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private PerformRulesCategoryService categoryService;
    @Autowired
    private UserService userService;


    public PerformRulesController() {
        super.setModuleDesc("绩效规则信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "subAddType", paramService.getParamBySecondOrganize("PerformRules", "subAddType"));
        this.addDict(model, "assoOwner", paramService.getParamBySecondOrganize("PerformRules", "assoOwner"));
        this.addDict(model, "type", paramService.getParamBySecondOrganize("PerformRules", "type"));
        this.addDict(model, "categoryId", categoryService.find(new PerformRulesCategoryQuery()).stream().collect(toMap(x -> x.getId().toString(), y -> y.getName(), (o, n) -> n)));
        this.addDict(model, "createUserId", userService.find(new UserQuery()).stream().collect(toMap(x -> x.getId().toString(), y -> y.getRealName(), (o, n) -> n)));
        super.init(model, context);
    }

    @Override
    protected void doListBefore(PerformRulesEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        query.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));
    }
}