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

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.YcqAppConfigUrl;
import com.crunii.micro.domain.repo.main.YcqAppConfigUrlRepo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AppConfigC;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AppConfigU;
import com.crunii.micro.service.sysmgr.jdbc.RoleJdbcDao;
import com.crunii.micro.service.sysmgr.service.AppHomeConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @author baoxy
 * @date 2023-06-09
 */
@Service
public class AppHomeConfigServiceImpl implements AppHomeConfigService {

    @Resource
    private YcqAppConfigUrlRepo ycqAppConfigUrlRepo;

    @Value("${upload.url}")
    private String urlPrefix;

    @Autowired
    private RoleJdbcDao roleJdbcDao;

    @Override
    public Result<List<YcqAppConfigUrl>> findAppConfigUrl() {
        List<YcqAppConfigUrl> ycqAppConfigUrlList = ycqAppConfigUrlRepo.findYcqAppConfigUrlByStateOrderBySortAsc(CommonConstants.STATE_VALID);
        ycqAppConfigUrlList.forEach(e -> e.setLogoUrl(urlPrefix + e.getLogoUrl()));
        return Result.ofSuccess(ycqAppConfigUrlList);
    }

    @Override
    public Result<Object> save(AppConfigC dto) {
        YcqAppConfigUrl ycqAppConfigUrl = saveAppConfigUrl(dto);
        ycqAppConfigUrlRepo.saveAndFlush(ycqAppConfigUrl);
        return Result.ofSuccess("调用成功!");
    }

    @Override
    public Result<Object> batchSave(List<AppConfigC> dtos) {
        List<YcqAppConfigUrl> appConfigList = new ArrayList<>();
        dtos.forEach(e -> {
            YcqAppConfigUrl ycqAppConfigUrl = saveAppConfigUrl(e);
            appConfigList.add(ycqAppConfigUrl);
        });
        ycqAppConfigUrlRepo.saveAllAndFlush(appConfigList);
        return Result.ofSuccess("调用成功!");
    }

    @Override
    public Result<Object> del(String appId) {
        YcqAppConfigUrl ycqAppConfigUrl = ycqAppConfigUrlRepo.getOne(Long.valueOf(appId));
        ycqAppConfigUrl.setState(CommonConstants.STATE_INVALID);
        ycqAppConfigUrlRepo.saveAndFlush(ycqAppConfigUrl);
        return Result.ofSuccess("调用成功!");
    }

    @Override
    public Result<Object> update(AppConfigU dto) {
        YcqAppConfigUrl ycqAppConfigUrl = ycqAppConfigUrlRepo.getOne(dto.getAppId());
        BeanUtils.copyProperties(dto, ycqAppConfigUrl);
        ycqAppConfigUrlRepo.saveAndFlush(ycqAppConfigUrl);
        return Result.ofSuccess("调用成功!");
    }

    @Override
    public Result<List<YcqAppConfigUrl>> findRoleApp(Worker worker) {
        List<YcqAppConfigUrl> ycqAppConfigUrlList = roleJdbcDao.findRoleApp(worker);
        ycqAppConfigUrlList.forEach(e -> e.setLogoUrl(urlPrefix + e.getLogoUrl()));
        return Result.ofSuccess(ycqAppConfigUrlList);
    }

    @Override
    public Result<List<YcqAppConfigUrl>> findRoleAppVisitor() {
        List<YcqAppConfigUrl> ycqAppConfigUrlList = roleJdbcDao.findRoleAppVisitor();
        ycqAppConfigUrlList.forEach(e -> e.setLogoUrl(urlPrefix + e.getLogoUrl()));
        return Result.ofSuccess(ycqAppConfigUrlList);
    }

    public YcqAppConfigUrl saveAppConfigUrl(AppConfigC e) {
        YcqAppConfigUrl ycqAppConfigUrl = new YcqAppConfigUrl();
        ycqAppConfigUrl.setAppCode(e.getAppCode());
        ycqAppConfigUrl.setAppUrl(e.getAppUrl());
        ycqAppConfigUrl.setAppName(e.getAppName());
        ycqAppConfigUrl.setCreateTime(new Date());
        ycqAppConfigUrl.setLogoUrl(e.getLogoUrl());
        ycqAppConfigUrl.setSort(e.getSort());
        ycqAppConfigUrl.setMustAuth(e.getMustAuth());
        ycqAppConfigUrl.setState(CommonConstants.STATE_VALID);
        return ycqAppConfigUrl;
    }
}
