package com.bonus.boot.manager.health.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonus.boot.manager.basic.entity.HospitalBean;
import com.bonus.boot.manager.health.entity.SpecialJobBean;
import com.bonus.boot.manager.health.entity.SpecialPositionsBean;
import com.bonus.boot.manager.health.service.SpecialJobService;
import com.bonus.boot.manager.manager.annotation.LogAnnotation;
import com.bonus.boot.manager.manager.entity.R;
import com.bonus.boot.manager.manager.utils.AjaxRes;
import com.bonus.boot.manager.manager.utils.GlobalConst;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 包名称：com.bonus.boot.manager.health.controller
 * 类名称：SpecialJobController
 * 类描述：特殊岗位配置管理
 * 创建人：@author tqzhang
 * 创建时间：2023年06月08日 13:53
 */

@RestController
@RequestMapping("specialJob")
public class SpecialJobController {


    @Resource(name = "specialJobService")
    private SpecialJobService service;


    @LogAnnotation
    @PostMapping("getMsgContent")
    @ApiOperation(value = "套餐-列表")
    public R getList(SpecialJobBean o){
        System.err.println("套餐-列表="+o);
        List<SpecialJobBean> list = service.getList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "特殊岗位配置管理-新增")
    public AjaxRes addInfo(SpecialJobBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.DATA_SUCCEED);
        }
        return  ar;
    }


    @LogAnnotation
    @PostMapping("/getSpecialJobTree")
    @ApiOperation(value = "特殊岗位树")
    public JSONArray listParentBuilder() {
        List<SpecialJobBean> permissionsAll = service.getSpecialJobTree();
        //每一个节点都当做第一个节点
        JSONArray array = new JSONArray();
        setPermissionsTree("0", permissionsAll, array);
        return array;
    }

    /**
     * 菜单树
     * @param pId
     * @param permissionsAll
     * @param array
     */
    private void setPermissionsTree(String pId, List<SpecialJobBean> permissionsAll, JSONArray array) {
        for (SpecialJobBean per : permissionsAll) {
            if (per.getParentId().equals(pId)) {
                String string = JSONObject.toJSONString(per);
                JSONObject parent = (JSONObject) JSONObject.parse(string);
                array.add(parent);

                if (permissionsAll.stream().filter(p -> p.getParentId().equals(per.getId())).findAny() != null) {
                    JSONArray child = new JSONArray();
                    parent.put("child", child);
                    setPermissionsTree(per.getId(), permissionsAll, child);
                }
            }
        }
    }
}
