package com.xinelu.manage.service.appfileinfo.impl;


import com.xinelu.common.config.XinELuConfig;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.file.FileUploadUtils;
import com.xinelu.common.utils.file.MimeTypeUtils;
import com.xinelu.manage.domain.appfileinfo.AppFileInfo;
import com.xinelu.manage.dto.appfileInfo.AppFileInfoDTO;
import com.xinelu.manage.mapper.appfileinfo.AppFileInfoMapper;
import com.xinelu.manage.service.appfileinfo.IAppFileInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 护理员App文件管理Service业务层处理
 *
 * @author xinyilu
 * @date 2022-11-15
 */
@Service
public class AppFileInfoServiceImpl implements IAppFileInfoService {
    @Resource
    private AppFileInfoMapper appFileInfoMapper;
    @Resource
    private XinELuConfig xinYiLuConfig;

    /**
     * 查询护理员App文件管理
     *
     * @param id 护理员App文件管理主键
     * @return 护理员App文件管理
     */
    @Override
    public AppFileInfo selectAppFileInfoById(Long id) {
        return appFileInfoMapper.selectAppFileInfoById(id);
    }

    /**
     * 查询护理员App文件管理列表
     *
     * @param appFileInfo 护理员App文件管理
     * @return 护理员App文件管理
     */
    @Override
    public List<AppFileInfo> selectAppFileInfoList(AppFileInfo appFileInfo) {
        return appFileInfoMapper.selectAppFileInfoList(appFileInfo);
    }

    /**
     * 新增护理员App文件管理
     *
     * @param appFileInfo 护理员App文件管理
     * @return 结果
     */
    @Override
    public int insertAppFileInfo(AppFileInfo appFileInfo) {
        appFileInfo.setCreateTime(LocalDateTime.now());
        return appFileInfoMapper.insertAppFileInfo(appFileInfo);
    }

    /**
     * 修改护理员App文件管理
     *
     * @param appFileInfo 护理员App文件管理
     * @return 结果
     */
    @Override
    public int updateAppFileInfo(AppFileInfo appFileInfo) {
        appFileInfo.setUpdateTime(LocalDateTime.now());
        return appFileInfoMapper.updateAppFileInfo(appFileInfo);
    }

    /**
     * 批量删除护理员App文件管理
     *
     * @param ids 需要删除的护理员App文件管理主键
     * @return 结果
     */
    @Override
    public int deleteAppFileInfoByIds(Long[] ids) {
        return appFileInfoMapper.deleteAppFileInfoByIds(ids);
    }

    /**
     * 护理员App上传apk文件
     *
     * @param appFileInfoDTO 文件
     * @return 结果
     */
    @Override
    public AjaxResult uploadAppFileInfo(AppFileInfoDTO appFileInfoDTO) throws Exception {
        if (StringUtils.isBlank(appFileInfoDTO.getFile().getOriginalFilename())
                || !appFileInfoDTO.getFile().getOriginalFilename().endsWith(Constants.APK)) {
            return AjaxResult.error("文件格式不对，请上传apk格式的文件！");
        }
        if (!StringUtils.equals(Constants.APK_NAME, appFileInfoDTO.getFile().getOriginalFilename())) {
            return AjaxResult.error("泉医到家护理员Apk文件名称不正确，请将文件名称修改为：" + Constants.APK_NAME);
        }
        //获取路径名称
        String uploadPathUrl = XinELuConfig.getProfile() + xinYiLuConfig.getAppFileName();
        //上传图片
        String fileLocalPath = FileUploadUtils.uploadPersonAppApkPath(uploadPathUrl, appFileInfoDTO.getFile(), MimeTypeUtils.IMAGE_APK);
        if (StringUtils.isBlank(fileLocalPath)) {
            throw new ServiceException("护理员App文件上传失败，请联系管理员！");
        }
        AppFileInfo appFileInfo = new AppFileInfo();
        appFileInfo.setFileName(Constants.APK_NAME);
        appFileInfo.setFileUrl(xinYiLuConfig.getAppFilePath());
        appFileInfo.setFileLocalPath(fileLocalPath);
        appFileInfo.setFileVersion(appFileInfoDTO.getVersion());
        appFileInfo.setCreateBy(SecurityUtils.getUsername());
        appFileInfo.setCreateTime(LocalDateTime.now());
        int insert = appFileInfoMapper.insertAppFileInfo(appFileInfo);
        if (insert <= 0) {
            throw new ServiceException("新增版本失败，请联系管理员！");
        }
        //获取返回值
        AjaxResult ajax = AjaxResult.success("上传成功！");
        ajax.put("imgUrl", fileLocalPath);
        return ajax;
    }
}