package com.engine.organization.service.impl;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.engine.core.impl.Service;
import com.engine.organization.entity.TreeData;
import com.engine.organization.entity.postion.dto.PostDTO;
import com.engine.organization.entity.postion.po.PostPO;
import com.engine.organization.mapper.post.PostMapper;
import com.engine.organization.service.PostService;
import com.engine.organization.util.HasRightUtil;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.OrganizationFormItemUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import weaver.general.StringUtil;
import weaver.general.Util;

import java.util.*;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/13
 * @version: 1.0
 */
public class PostServiceImpl extends Service implements PostService {

    private static final String RIGHT_NAME = "Post:All";

    private PostMapper getPostMapper() {
        return MapperProxyFactory.getProxy(PostMapper.class);
    }

    @Override
    public int savePost(PostPO postPO) {
        HasRightUtil.hasRight(user, RIGHT_NAME, false);
        // List<PostPO> list = getPostMapper().listByNo(Util.null2String(postPO.getPostNo()));
        // OrganizationAssert.isEmpty(list, "编号不允许重复");
        return getPostMapper().insertIgnoreNull(PostDTO.convertPO(postPO, user.getUID()));
    }

    @Override
    public int updatePost(PostPO postPO) {
        HasRightUtil.hasRight(user, RIGHT_NAME, false);
        return getPostMapper().updatePost(PostDTO.convertPO(postPO, user.getUID()));
    }

    @Override
    public int deleteByIds(Collection<Long> ids) {
        HasRightUtil.hasRight(user, RIGHT_NAME, false);
        OrganizationAssert.notEmpty(ids, "请选择要删除的数据");
        return getPostMapper().deleteByIds(ids);
    }

    @Override
    public Map<String, Object> getPostForm(Map<String, Object> params) {
        HasRightUtil.hasRight(user, RIGHT_NAME, false);
        Map<String, Object> apiDatas = new HashMap<>();
        List<SearchConditionItem> selectItems = new ArrayList<>();
        List<SearchConditionGroup> addGroups = new ArrayList<>();
        SearchConditionItem postNameItem = OrganizationFormItemUtil.inputItem(user, 2, 17, 3, 50, "名称", "postName");
        postNameItem.setRules("required|string");
        SearchConditionItem descriptionItem = OrganizationFormItemUtil.textareaItem(user, 2, 17, true, 2, 200, "描述说明", "description");

        // 编辑状态下赋值操作
        String id = Util.null2String(params.get("id"));
        if (!StringUtil.isEmpty(id)) {
            PostPO postPO = getPostMapper().getPostByID(Integer.parseInt(id));
            OrganizationAssert.notNull(postPO, "选择的数据不存在，或数据已删除");

            postNameItem.setValue(postPO.getPostName());
            descriptionItem.setValue(postPO.getDescription());

        }

        selectItems.add(postNameItem);
        selectItems.add(descriptionItem);
        addGroups.add(new SearchConditionGroup("基本信息", true, selectItems));
        apiDatas.put("condition", addGroups);
        return apiDatas;
    }

    @Override
    public TreeData getTreeData() {
        ArrayList<TreeData> treeDataList = getPostMapper().getTreeData();
        return TreeData.builder().children(treeDataList).title("职务分类").key("-1").build();
    }


}
