package org.jeecg.modules.ctop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ctop.entity.UReportFileBindUser;
import org.jeecg.modules.ctop.mapper.UReportFileBindUserMapper;
import org.jeecg.modules.ctop.service.IUReportFileBindUserService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

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

/**
 * 用户绑定报表信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-01-16
 */
@Service
public class UReportFileBindUserServiceImpl extends ServiceImpl<UReportFileBindUserMapper, UReportFileBindUser> implements IUReportFileBindUserService {

    @Override
    public UReportFileBindUser getByUserIdAndFileId(String userId, String fileId) {
        QueryWrapper<UReportFileBindUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("file_id", fileId)
                .orderByDesc("id")
                .last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public void deleteByUserIdAndFileId(String userId, String fileId) {
        QueryWrapper<UReportFileBindUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("file_id", fileId);
        this.remove(queryWrapper);
    }

    @Override
    public Map<String, Object> getListByFileId(JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        if (null == data || null == data.getString("fileId") || "".equals(data.getString("fileId"))) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        String fileId = data.getString("fileId");
        QueryWrapper<UReportFileBindUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_id", fileId);
        List<UReportFileBindUser> bindUsers = this.list(queryWrapper);
        result.put("data", bindUsers);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }
}
