package vip.xiaonuo.write.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.common.spring.SpelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.production.entity.ProductionWriteRate;
import vip.xiaonuo.sys.modular.user.entity.SysUser;
import vip.xiaonuo.sys.modular.user.mapper.SysUserMapper;
import vip.xiaonuo.write.entity.WriteThreshold;
import vip.xiaonuo.write.mapper.WriteThresholdMapper;
import vip.xiaonuo.write.param.WriteThresholdAddParam;
import vip.xiaonuo.write.param.WriteThresholdPageParam;
import vip.xiaonuo.write.service.IWriteThresholdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 通用预警阈值 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-01
 */
@Service
public class WriteThresholdServiceImpl extends ServiceImpl<WriteThresholdMapper, WriteThreshold> implements IWriteThresholdService {

    @Autowired
    private WriteThresholdMapper thresholdMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public CommonResult add(WriteThresholdAddParam addParam) {
        WriteThreshold threshold = new WriteThreshold();
        BeanUtil.copyProperties(addParam, threshold);
        thresholdMapper.insert(threshold);
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(WriteThreshold threshold) {
        thresholdMapper.updateById(threshold);
        return CommonResult.ok();
    }

    @Override
    public CommonResult delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        thresholdMapper.deleteById(id);
        return CommonResult.ok();
    }

    @Override
    public Page<WriteThreshold> page(WriteThresholdPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        QueryWrapper<WriteThreshold> queryWrapper = new QueryWrapper();
        Page<WriteThreshold> page = new Page<>(pageNum, pageSize);
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId).eq("type_s",pageParam.getType());
        return thresholdMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysUser> getUserList(Map<String, String> paramsMap) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        String userId = loginUser.getId();
        SysUser sysUser = sysUserMapper.selectById(userId);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CREATE_USER", sysUser.getId());
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
        sysUsers.add(sysUser);
        return sysUsers;
    }

}
