package com.bonus.material.ma.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.ma.domain.Type;
import com.bonus.material.ma.domain.vo.MachineVo;
import com.bonus.material.purchase.config.RemoteConfig;
import com.bonus.system.api.RemoteUserService;
import com.bonus.system.api.domain.SysUser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.bonus.material.ma.mapper.MachineMapper;
import com.bonus.material.ma.domain.Machine;
import com.bonus.material.ma.service.IMachineService;

import javax.annotation.Resource;

/**
 * 机具设备管理Service业务层处理
 * 
 * @author xsheng
 * @date 2024-09-27
 */
@Service
@Slf4j
public class MachineServiceImpl implements IMachineService 
{
    @Resource
    private MachineMapper machineMapper;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private RemoteConfig remoteConfig;

    /**
     * 查询机具设备管理
     * 
     * @param maId 机具设备管理主键
     * @return 机具设备管理
     */
    @Override
    public MachineVo selectMachineByMaId(Long maId)
    {
        MachineVo machineVo = machineMapper.selectMachineByMaId(maId);
        setUserName(machineVo.getKeeperId(), machineVo::setKeeperName);
        setUserName(machineVo.getRepairId(), machineVo::setRepairName);
        Map<String, String> machineStatus = remoteConfig.getDictValue("ma_machine_status");
        if (StringUtils.isNotBlank(machineVo.getMaStatus())) {
            machineVo.setStatusName(StringUtils.isBlank(machineStatus.get(machineVo.getMaStatus())) ? "" : machineStatus.get(machineVo.getMaStatus()));
        }
        return machineVo;
    }

    private void setUserName(Long userId, Consumer<String> setNameFunction) {
        try {
            AjaxResult ajaxResult = remoteUserService.getInfo(userId, SecurityConstants.INNER);
            if (ajaxResult.isSuccess()) {
                // ajaxResult.get("data") 返回的是 LinkedHashMap
                LinkedHashMap<String, Object> rawDataList = (LinkedHashMap<String, Object>) ajaxResult.get("data");
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                if (rawDataList != null) {
                    SysUser sysUser = objectMapper.convertValue(rawDataList, SysUser.class);
                    setNameFunction.accept(sysUser.getNickName() == null ? "" : sysUser.getNickName());
                }
            }
        } catch (IllegalArgumentException e) {
           log.error("远程调用查询失败：", e.getMessage());
        }
    }

    /**
     * 查询机具设备管理列表
     * 
     * @param machine 机具设备管理
     * @return 机具设备管理
     */
    @Override
    public List<MachineVo> selectMachineList(Machine machine)
    {
        List<MachineVo> list = machineMapper.selectMachineList(machine);
        Map<String, String> machineStatus = remoteConfig.getDictValue("ma_machine_status");
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(machineVo -> {
                if (StringUtils.isNotBlank(machineVo.getMaStatus())) {
                    machineVo.setStatusName(StringUtils.isBlank(machineStatus.get(machineVo.getMaStatus())) ? "" : machineStatus.get(machineVo.getMaStatus()));
                }
            });
        }
        return list;
    }

    /**
     * 新增机具设备管理
     * 
     * @param machine 机具设备管理
     * @return 结果
     */
    @Override
    public int insertMachine(Machine machine)
    {
        machine.setCreateTime(DateUtils.getNowDate());
        return machineMapper.insertMachine(machine);
    }

    /**
     * 修改机具设备管理
     * 
     * @param machine 机具设备管理
     * @return 结果
     */
    @Override
    public AjaxResult updateMachine(Machine machine)
    {
        machine.setUpdateTime(DateUtils.getNowDate());
        int result = machineMapper.updateMachine(machine);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 批量删除机具设备管理
     * 
     * @param maIds 需要删除的机具设备管理主键
     * @return 结果
     */
    @Override
    public int deleteMachineByMaIds(Long[] maIds)
    {
        return machineMapper.deleteMachineByMaIds(maIds);
    }

    /**
     * 删除机具设备管理信息
     * 
     * @param maId 机具设备管理主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteMachineByMaId(Long maId)
    {
        int result = machineMapper.deleteMachineByMaId(maId);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 根据类型查询
     * @param type
     * @return
     */
    @Override
    public AjaxResult selectByTypeList(Type type) {
        List<Type> typeList = machineMapper.selectByTypeList(type);
        return AjaxResult.success(typeList);
    }
}
