package thyyxxk.webserver.service.reports;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.reports.ReportQueryCenterDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.reports.reportQueryCenter.ReportForms;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportQueryCenterService {

    @Data
    public static class PersonRoleCode {
        private String userCode;
        private List<String> list;
    }

    private final ReportQueryCenterDao dao;
    private final PublicServer publicServer;

    public ReportQueryCenterService(ReportQueryCenterDao dao, PublicServer publicServer) {
        this.dao = dao;
        this.publicServer = publicServer;
    }

    public ResultVo<String> addReportQueryCenter(ReportForms param) {
//        if (StringUtil.isBlank(param.getName())) {
//            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "名称不能为空。");
//        }
//        param.setId(SnowFlakeId.instance().nextId());
//        dao.insert(param);
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

    public ResultVo<List<ReportForms>> getReportTree() {
        List<ReportForms> list;
        if (publicServer.needRule()) {
            list = dao.selectReportFormsAll();
        } else {
            list = dao.selectReportForms(TokenUtil.getInstance().getTokenUserId());
        }
        return ResultVoUtil.success(toTree(list));
    }

    public ResultVo<String> updateReportQueryCenter(ReportForms param) {
//        dao.updateById(param);
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

    public ResultVo<String> delReportQueryCenterById(String id) {
//        dao.del(id);
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

    private List<ReportForms> toTree(List<ReportForms> list) {
        List<ReportForms> tree = new ArrayList<>();
        Map<String, ReportForms> map = new HashMap<>();
        for (ReportForms item : list) {
            if (StringUtil.isBlank(item.getParentId())) {
                tree.add(item);
            }
            map.put(item.getId(), item);
        }

        for (ReportForms item : list) {
            if (StringUtil.notBlank(item.getParentId())) {
                ReportForms parent = map.get(item.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(item);
                }
            }
        }
        return tree;
    }

    public ResultVo<List<String>> getRulesByCode(String userCode) {
        return ResultVoUtil.success(dao.getRulesByCode(userCode));
    }

    public ResultVo<String> updateParentById(String id, String parentId) {
//        if (StringUtil.isBlank(parentId)) {
//            parentId = null;
//        }
//        dao.updateParentById(id, parentId);
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "操作成功。");
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

    public ResultVo<String> saveThePersonRoleCode(PersonRoleCode data) {
//        dao.delRules(data.getUserCode());
//        ListUtil.batchList(data.getList(), ReportQueryCenterDao.class, (mapper, item) -> {
//            mapper.insertRules(data.getUserCode(), item);
//        });
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "操作成功。");
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

    public ResultVo<String> updateName(String id, String name) {
//        ReportForms reportForms = new ReportForms();
//        reportForms.setId(id);
//        reportForms.setName(name);
//        dao.updateById(reportForms);
//        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此功能已停用，请使用数据中台中的页面编辑。");
    }

}
