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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.util.StringUtils;
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.staff.service.StaffService;
import com.mortals.xhx.module.workman.model.WorkmanQuery;
import com.mortals.xhx.module.workman.service.WorkmanService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mortals.framework.ap.SysConstains.RESULT_KEY;

/**
 * 部门信息
 *
 * @author zxfei
 * @date 2023-03-06
 */
@RestController
@RequestMapping("dept")
public class DeptController extends BaseCRUDJsonBodyMappingController<DeptService, DeptEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private WorkmanService workmanService;
    @Autowired
    private StaffService staffService;


    public DeptController() {
        super.setModuleDesc("部门信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "deptStatus", paramService.getParamBySecondOrganize("Dept", "deptStatus"));
        this.addDict(model, "deptName", deptService.find(new DeptQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getDeptName(),(o, n)->n)));
        this.addDict(model, "workName", workmanService.find(new WorkmanQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getName(),(o, n)->n)));
        super.init(model, context);
    }

    @PostMapping("list/exclude")
    public String excludeList(@RequestBody DeptEntity deptEntity) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        // Long id = form.getId()[0];
        int code = VALUE_RESULT_SUCCESS;
        try {
            List<DeptEntity> collect = this.service.find(new DeptQuery()).stream().map(item -> {
                if (item.getId().intValue() == deptEntity.getId() || ArrayUtils.contains(StringUtils.split(item.getAncestors(), ","), deptEntity.getId() + "")) {
                    return null;
                }
                return item;
            }).filter(f -> f != null).collect(Collectors.toList());
            model.put("result", collect);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 获取站点下拉树列表
     */
    @PostMapping("treeselect")
    public String treeselect() {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code = VALUE_RESULT_SUCCESS;
        try {
            DeptQuery deptQuery = new DeptQuery();
            deptQuery.setOrderColList(Arrays.asList(new OrderCol("deptName")));
            List<DeptEntity> list = this.service.find(deptQuery);
            List<DeptTreeSelect> treeSelects = this.service.buildDeptTreeSelect(list);
            model.put("result", treeSelects);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 根据parentId查询子信息
     */
    @GetMapping(value = "getListByParentId")
    public String getListByParentId(Long parentId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            List<DeptTreeSelect> treeList = this.service.getListByParentId(parentId, getContext());
            model.put(RESULT_KEY, treeList);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + "【成功】");
        } catch (Exception e) {
            log.error("根据parentId查询子信息错误", e);
            this.doException(request, busiDesc, model, e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }


    @Override
    protected int doListAfter(DeptEntity query, Map<String, Object> model, Context context) throws AppException {
        int allPerson =staffService.queryAllPerson();
        model.put("allPerson",allPerson);

        return super.doListAfter(query, model, context);
    }

    /**
     * 查询大厅列表
     * @return
     */
    @GetMapping({"getSalaList"})
    public Rest<Object> getAllSala() {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询大厅列表";
        int code = 1;
        try {
            List<DeptEntity> allSala = this.getService().getAllSala();
            model.put("data", allSala);
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model);
        ret.setDict(model.get("dict"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

    /**
     * 查询大厅列表
     * @return
     */
    @GetMapping({"getDeptBySalaId"})
    public Rest<Object> getDeptBySalaId(Long salaId) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询大厅对应的部门列表";
        int code = 1;
        try {
            List<DeptEntity> allDept = this.getService().getDeptBySalaId(salaId);
            model.put("data", allDept);
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model);
        ret.setDict(model.get("dict"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

}