package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.dto.AppBaseDTO;
import com.om.entity.dto.GuidDTO;
import com.om.entity.po.Manual;
import com.om.entity.vo.AppManual;
import com.om.exception.BadReqException;
import com.om.mapper.ManualMapper;
import com.om.service.IManualService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.AesUtil;
import com.om.utils.JwtUtils;
import com.om.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneOffset;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author henry-ong
 * @since 2024-02-28
 */
@Service
public class ManualServiceImpl extends ServiceImpl<ManualMapper, Manual> implements IManualService {
  private final ManualMapper manualMapper;

  public ManualServiceImpl(ManualMapper manualMapper) {
    this.manualMapper = manualMapper;
  }

  @Resource
  private JwtUtils jwtUtils;

  @Override
  public Page<Manual> findManual(Page<Manual> page, String code, String title) {
    return manualMapper.findManual(page, code, title);
  }

    @Override
    public Result<AppManual> AppGetByLang(AppBaseDTO dto) {
        String language = dto.getLanguage();
        String guid = dto.getGuid();
        GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
        String clientNum = guidDTO.getClientNum();
        Manual one = this.lambdaQuery()
                .eq(Manual::getLang, language)
                .eq(Manual::getClientCode,clientNum)
                .one();

        if (BeanUtil.isEmpty(one)){
            throw new BadReqException("该使用手册不存在");
        }
        AppManual vo = new AppManual();
        BeanUtil.copyProperties(one, vo);
        vo.setCreateTime(one.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return Result.ok(vo);
    }
}
