package com.crunii.micro.service.sysmgr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.YcqAppVersion;
import com.crunii.micro.domain.repo.main.YcqAppVersionRepo;
import com.crunii.micro.domain.util.CurrWorkerUtil;
import com.crunii.micro.service.sysmgr.dto.sysmgr.YcqAppVersionDto;
import com.crunii.micro.service.sysmgr.dto.sysmgr.YcqAppVersionVo;
import com.crunii.micro.service.sysmgr.jdbc.YcqAppVersionDao;
import com.crunii.micro.service.sysmgr.service.YcqAppVersionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author baoxy
 * @date 2023-07-11
 */
@Service
public class YcqAppVersionServiceImpl implements YcqAppVersionService {

    @Resource
    private YcqAppVersionDao dao;

    @Resource
    private YcqAppVersionRepo repo;


    @Override
    public Result<YcqAppVersionVo> getMaxVersion() {
        return Result.ofSuccess(dao.getMaxAppVersion());
    }

    @Override
    public Result<Object> create(YcqAppVersionDto dto) {
        Worker worker = getWorker();
        YcqAppVersion appVersion = new YcqAppVersion();
        appVersion.setFileUrl(dto.getFileUrl());
        appVersion.setVersion(dto.getVersion());
        appVersion.setUpdatesType(dto.getUpdatesType());
        appVersion.setIsAvailable(1);
        appVersion.setPublishTime(dto.getPublishTime());
        appVersion.setMinimumVersion(dto.getMinimumVersion());
        appVersion.setClientType(dto.getClientType());
        appVersion.setPublicationType(dto.getPublicationType());
        appVersion.setContent(dto.getContent());
        appVersion.setVersionName(dto.getVersionName());
        appVersion.setCreatedBy(worker.getLoginId() + "");
        appVersion.setCreatedTime(new Date());
        appVersion.setContent(dto.getContent());
        repo.save(appVersion);
        return Result.ofSuccess();
    }

    @Override
    public Result<Object> update(YcqAppVersionDto dto) {
        Worker worker = getWorker();
        YcqAppVersion appVersion = new YcqAppVersion();
        appVersion.setId(dto.getId());
        appVersion.setFileUrl(dto.getFileUrl());
        appVersion.setVersion(dto.getVersion());
        appVersion.setUpdatesType(dto.getUpdatesType());
        appVersion.setIsAvailable(1);
        appVersion.setPublishTime(dto.getPublishTime());
        appVersion.setClientType(dto.getClientType());
        appVersion.setPublicationType(dto.getPublicationType());
        appVersion.setMinimumVersion(dto.getMinimumVersion());
        appVersion.setVersionName(dto.getVersionName());
        appVersion.setCreatedBy(worker.getLoginId() + "");
        appVersion.setCreatedTime(new Date());
        appVersion.setContent(dto.getContent());
        repo.save(appVersion);
        return Result.ofSuccess();
    }

    @Override
    public Result<Object> delete(Long id) {
        YcqAppVersion ycqAppVersion = repo.getOne(id);
        if (ycqAppVersion==null){
            return Result.ofFail("没有对应版本");
        }
        repo.deleteById(id);
        return Result.ofSuccess();
    }

    @Override
    public Result<PageRsp<YcqAppVersionVo>> getPageAppVersion(PageReq<YcqAppVersionDto> req) {
        return Result.ofSuccess(dao.getPageAppVersion(req));
    }


    @Override
    public Result<List<YcqAppVersionVo>> getListAppVersion(YcqAppVersionDto dto) {
        return Result.ofSuccess(dao.getListAppVersion(dto));
    }

    private Worker getWorker() {
        Worker worker = CurrWorkerUtil.getCurrWorker();
        if (ObjectUtil.isEmpty(worker)) {
            throw new RuntimeException("获取当前登录用户失败");
        }
        return worker;

    }
}
