package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.vo.AssetVo;
import com.ruoyi.system.mapper.AssetlogMapper;
import com.ruoyi.system.domain.Assetlog;
import com.ruoyi.system.service.AssetlogService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

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

/**
 * (Assetlog)表服务实现类
 *
 * @author makejava
 * @since 2024-05-28 10:47:18
 */
@Service
public class AssetlogServiceImpl implements AssetlogService {
    @Resource
    private AssetlogMapper assetlogMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Assetlog queryById(Integer id) {
        return this.assetlogMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param assetlog 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Assetlog> queryByPage(Assetlog assetlog, PageRequest pageRequest) {
        long total = this.assetlogMapper.count(assetlog);
        return new PageImpl<>(this.assetlogMapper.queryAllByLimit(assetlog, pageRequest), pageRequest, total);
    }

    @Override
    public List<Assetlog> list(AssetVo assetVo) {

//        return assetlogMapper.list(assetVo, Convert.toIntArray(assetVo.getDutyId()));
        return assetlogMapper.newList(assetVo);
    }

    @Override
    public List<Map<String, Object>> getAssetType(Integer departmentId, Integer type) {
        if (type != null && type == 1) {
            return assetlogMapper.sumAssetType(departmentId);
        }
        return assetlogMapper.getAssetType(departmentId);
    }

    @Override
    public List<Map<String, Object>> getAssetSource(Integer departmentId) {
        return assetlogMapper.getAssetSource(departmentId);
    }

    @Override
    public List<Map<String, Object>> getAssetBorrow(Integer assetId) {
        return assetlogMapper.getAssetBorrow(assetId);
    }

    @Override
    public List<Map<String, Object>> getAssetGiveBack(Integer assetId) {
        return assetlogMapper.getAssetGiveBack(assetId);
    }

    @Override
    public List<Map<String, Object>> getAssetRepair(Integer assetId) {
        return assetlogMapper.getAssetRepair(assetId);
    }

    @Override
    public List<Map<String, Object>> getAssetScrap(Integer assetId) {
        return assetlogMapper.getAssetScrap(assetId);
    }

    @Override
    public Map<String, Object> mobileLogin(String username, String password) {
        String pwd = Md5Utils.hash(password);
        List<Map<String, Object>> userList = assetlogMapper.mobileLogin(username, pwd);
        if (userList != null && userList.size() == 1) {
            Map<String, Object> result = userList.get(0);
            result.put("Password", "");
            return result;
        }
        return null;
    }
}
