package com.sckw.mine.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.utils.TenantUtil;
import com.sckw.core.web.response.HttpResult;
import com.sckw.mine.common.OrderServerCommon;
import com.sckw.mine.entity.KwBusinessMineralAggregate;
import com.sckw.mine.entity.KwBusinessNode;
import com.sckw.mine.entity.req.*;
import com.sckw.mine.entity.res.KwBusinessMineralAggregateDetailRes;
import com.sckw.mine.entity.res.MineralAggregateListRes;
import com.sckw.mine.entity.res.NodeDetailRes;
import com.sckw.mine.entity.res.NodePageListRes;
import com.sckw.mine.enums.MineralAggregateTypeEnum;
import com.sckw.mine.enums.NodeTypeEnum;
import com.sckw.mine.mapper.NodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-05-31
 */
@Service
public class NodeService {

    @Autowired
    NodeMapper nodeMapper;

    @Autowired
    OrderServerCommon orderServerCommon;

    /*
    矿料添加
     */
    public HttpResult insert(NodeInsertParam param) {

        if (param == null) {
            return HttpResult.error("必要参数缺失");
        }

        LambdaQueryWrapper<KwBusinessNode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwBusinessNode::getNodeName, param.getNodeName())
                .eq(KwBusinessNode::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessNode kwBusinessNode = nodeMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(kwBusinessNode)) {
            throw new RuntimeException("当前节点已存在,勿重复添加");
        }

        KwBusinessNode kwBusinessNodeInsert = BeanUtil.copyProperties(param, KwBusinessNode.class);
        String code = orderServerCommon.generatorNum("ND", 13);
        String tenant = TenantUtil.getTenant();

        kwBusinessNodeInsert.setId(new IdWorker(1L).nextId());
        kwBusinessNodeInsert.setTenantId(tenant);
        kwBusinessNodeInsert.setNodeCode(code);
        kwBusinessNodeInsert.setStatus(1);

        nodeMapper.insert(kwBusinessNodeInsert);
        return HttpResult.ok();
    }


    public HttpResult nodeDelete(String id) {
        int ret = nodeMapper.nodeDelete(id);
        return ret>0?HttpResult.ok():HttpResult.error();
    }


    public HttpResult update(NodeUpdateParam param) {

        LambdaQueryWrapper<KwBusinessNode> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(KwBusinessNode::getId, param.getId())
                .eq(KwBusinessNode::getNodeName, param.getNodeName())
                .eq(KwBusinessNode::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessNode kwBusinessNode = nodeMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(kwBusinessNode)){
            throw new RuntimeException("当前名称已存在");
        }

        int ret = nodeMapper.nodeUpdate(param);
        return ret>0?HttpResult.ok():HttpResult.error();
    }


    public NodeDetailRes detail(String id) {
        NodeDetailRes load = nodeMapper.load(id);
        load.setStatusStr(load.getStatus()==1?"启用":"禁用");
        load.setNodeTypeStr(NodeTypeEnum.getNameByCode(load.getNodeType()));
        return load;
    }


    public HttpResult pageList(NodePageListParam param) {

        String tenant = TenantUtil.getTenant();
        if (StringUtils.isNotBlank(tenant)) {
            param.setTenantId(tenant);
        }
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<NodePageListRes> businessTruckPageListRes = nodeMapper.nodePageList(param);
        businessTruckPageListRes.forEach(e -> {
            e.setNodeTypeStr(NodeTypeEnum.getNameByCode(e.getNodeType()));
            e.setStatusStr(e.getStatus()==1?"启用":"禁用");
        });
        return HttpResult.ok(new PageRes<>(new PageInfo<>(businessTruckPageListRes)));
    }

    /**
    * @Description: 状态操作(停用 启用)   1=启用   2=禁用
    * @Author: Lt
    * @Date: 2024/5/31 11:34
    */
    public HttpResult statusAction(String id, Integer status)
    {
        if (StringUtils.isBlank(id)) {
            throw new RuntimeException("id必填");
        }
        if(status==null) {
            throw new RuntimeException("状态必填");
        }
        KwBusinessNode kwBusinessNode = nodeMapper.selectById(id);
        if (ObjectUtils.isNull(kwBusinessNode)) {
            throw new RuntimeException("当前数据不存在,请检查");
        }

        int i = nodeMapper.statusAction(id, status);
        if (i>0) {
            return HttpResult.ok("操作成功");
        }
        return HttpResult.error("操作失败");
    }

}
