package com.red.circle.other.app.inner.service.sys.impl;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.sys.StartPagePlanInnerConvertor;
import com.red.circle.other.app.inner.service.sys.StartPagePlanClientService;
import com.red.circle.other.infra.database.rds.entity.sys.StartPagePlan;
import com.red.circle.other.infra.database.rds.service.sys.StartPagePlanService;
import com.red.circle.other.inner.model.cmd.sys.SysStartPagePlanCmd;
import com.red.circle.other.inner.model.cmd.sys.SysStartPagePlanQryCmd;
import com.red.circle.other.inner.model.dto.sys.SysStartPagePlanDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 启动页展示图片 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2021-07-08
 */
@Service
@RequiredArgsConstructor
public class StartPagePlanClientServiceImpl implements StartPagePlanClientService {

  private final StartPagePlanService startPagePlanService;
  private final StartPagePlanInnerConvertor startPagePlanInnerConvertor;

  @Override
  public PageResult<SysStartPagePlanDTO> pageStartPage(SysStartPagePlanQryCmd query) {
    return startPagePlanService.pageStartPage(query)
        .convert(startPagePlanInnerConvertor::toSysStartPagePlanDTO);
  }

  @Override
  public void update(SysStartPagePlanCmd param) {
    ResponseAssert.notNull(ResponseErrorCode.REQUEST_PARAMETER_ERROR, param.getId());
    StartPagePlan startPagePlan = convertor(param);
    startPagePlan.setExpireTime(TimestampUtils.convert(param.getExpireTime()));
    startPagePlanService.updateSelectiveById(startPagePlan);
  }

  @Override
  public void add(SysStartPagePlanCmd param) {
    ResponseAssert.isNull(ResponseErrorCode.REQUEST_PARAMETER_ERROR, param.getId());
    StartPagePlan startPagePlan = convertor(param);
    startPagePlan.setExpireTime(TimestampUtils.convert(param.getExpireTime()));
    startPagePlanService.save(startPagePlan);
  }

  private StartPagePlan convertor(SysStartPagePlanCmd cmd) {
    if (cmd == null) {
      return null;
    }

    return new StartPagePlan()
        .setId(cmd.getId())
        .setSysOrigin(cmd.getSysOrigin())
        .setType(cmd.getType())
        .setCover(cmd.getCover());
  }

  @Override
  public void delete(Long id) {
    ResponseAssert.notNull(ResponseErrorCode.REQUEST_PARAMETER_ERROR, id);
    startPagePlanService.delete().eq(StartPagePlan::getId, id).execute();
  }

}
