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

import com.crunii.micro.common.constants.CommonConstants;
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.YcqAppConfigUrl;
import com.crunii.micro.domain.repo.main.YcqAppConfigUrlRepo;
import com.crunii.micro.service.sysmgr.constants.SysmgrConstants;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessCreateDto;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessPageDto;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessPageVo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessUpdateDto;
import com.crunii.micro.service.sysmgr.jdbc.AccessJdbcDao;
import com.crunii.micro.service.sysmgr.service.AccessService;
import org.springframework.beans.BeanUtils;
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;
import java.util.Objects;

/**
 * @author baoxy
 * @date 2023-07-11
 */
@Service
public class AccessServiceImpl implements AccessService {

    @Resource
    private AccessJdbcDao accessJdbcDao;

    @Resource
    private YcqAppConfigUrlRepo ycqAppConfigUrlRepo;

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

    @Override
    public Result<PageRsp<AccessPageVo>> pageSearch(PageReq<AccessPageDto> req) {
        PageRsp<AccessPageVo> rsp = accessJdbcDao.pageSearch(req);
        List<AccessPageVo> result = rsp.getResult();
        result.forEach(e -> e.setLogoUrl(urlPrefix + e.getLogoUrl()));
        return Result.ofSuccess(rsp);
    }

    @Override
    public Result<Object> create(AccessCreateDto dto, Worker worker) {
        Result<Object> checkRepeat = checkRepeat(dto.getLogoUrl(), dto.getAppName(), String.valueOf(dto.getSort()));
        if (Objects.nonNull(checkRepeat)) {
            return checkRepeat;
        }
        YcqAppConfigUrl ycqAppConfigUrl = new YcqAppConfigUrl();
        BeanUtils.copyProperties(dto, ycqAppConfigUrl);
        commonSaveDeal(dto.getAppUrl(), ycqAppConfigUrl, worker.getLoginId());
        // 将图标地址logoUrl的地址中的/转换为%2f
        ycqAppConfigUrl.setLogoUrl(ycqAppConfigUrl.getLogoUrl());
        ycqAppConfigUrl.setCreateTime(new Date());
        ycqAppConfigUrl.setSponsor(Long.valueOf(dto.getSponsor()));
        ycqAppConfigUrlRepo.saveAndFlush(ycqAppConfigUrl);
        return Result.ofSuccess("调用成功!");
    }

    @Override
    public Result<Object> update(AccessUpdateDto dto, Worker worker) {
        Result<Object> checkRepeat = checkRepeatUpdateState(dto.getLogoUrl(), dto.getAppName(), String.valueOf(dto.getSort()),
                String.valueOf(dto.getAppId()));
        if (Objects.nonNull(checkRepeat)) {
            return checkRepeat;
        }
        YcqAppConfigUrl one = ycqAppConfigUrlRepo.getOne(Long.valueOf(dto.getAppId()));
        YcqAppConfigUrl ycqAppConfigUrl = new YcqAppConfigUrl();
        String updateLogUrl;
        // 校验图片地址logoUrl是否有改动来修改, 先将传来的logoUrl删除前缀
        if (dto.getLogoUrl().replace(urlPrefix, "").equals(one.getLogoUrl())) {
            updateLogUrl = one.getLogoUrl();
        } else {
            updateLogUrl = dto.getLogoUrl();
        }
        BeanUtils.copyProperties(dto, ycqAppConfigUrl);
        ycqAppConfigUrl.setLogoUrl(updateLogUrl);
        ycqAppConfigUrl.setCreateTime(one.getCreateTime());
        ycqAppConfigUrl.setAppId(one.getAppId());
        ycqAppConfigUrl.setSponsor(Long.valueOf(dto.getSponsor()));
        commonSaveDeal(dto.getAppUrl(), ycqAppConfigUrl, worker.getLoginId());
        ycqAppConfigUrlRepo.saveAndFlush(ycqAppConfigUrl);
        return Result.ofSuccess("调用成功");
    }

    @Override
    public Result<Object> updateState(String appId, Worker worker) {
        YcqAppConfigUrl ycqAppConfigUrl = accessJdbcDao.getState(appId);
        int state = 0;
        if (CommonConstants.STATE_INVALID == ycqAppConfigUrl.getState()) {
            state = CommonConstants.STATE_VALID;
        }
        accessJdbcDao.updateState(appId, state, worker.getLoginId());
        return Result.ofSuccess("调用成功");
    }

    @Override
    public Result<List<AccessPageVo>> list(Integer loginId) {
        List<AccessPageVo> rsp = accessJdbcDao.list(loginId);
        return Result.ofSuccess(rsp);
    }

    @Override
    public Result<List<AccessPageVo>> getTrainBusinessType(int loginId) {
        List<AccessPageVo> rsp = accessJdbcDao.list(loginId);
        if (rsp==null){
            rsp=new ArrayList<>();
        }
        AccessPageVo accessPageVo = new AccessPageVo();
        accessPageVo.setAppName("其他");
        accessPageVo.setAppCode("qita");
        accessPageVo.setSystemName("其他");
        rsp.add(accessPageVo);

        AccessPageVo tzzjs = new AccessPageVo();
        tzzjs.setAppName("团组织建设");
        tzzjs.setAppCode("tzzjs");
        tzzjs.setSystemName("团组织建设");
        rsp.add(tzzjs);
        return Result.ofSuccess(rsp);
    }

    public Result<Object> checkRepeat(String logoUrl, String appName, String sort) {
        // 进行功能图标的数据重复校验,若值为0以上则返回页面提醒有重复数据
        int logoUrlCount = accessJdbcDao.checkRepeat(SysmgrConstants.LOGO_URL_FIELD, logoUrl);
        if (logoUrlCount > 0) {
            return Result.ofFail("功能图标有重复数据,请重新输入");
        }
        // 进行功能名称的数据重复校验
        int appNameCount = accessJdbcDao.checkRepeat(SysmgrConstants.APP_NAME_FIELD, appName);
        if (appNameCount > 0) {
            return Result.ofFail("功能名称有重复数据,请重新填写");
        }
        // 进行排序数据重复校验
        int sortCount = accessJdbcDao.checkRepeat(SysmgrConstants.SORT, sort);
        if (sortCount > 0) {
            return Result.ofFail("排序有重复数据,请重新填写");
        }
        return null;
    }

    public void commonSaveDeal(String appUrl, YcqAppConfigUrl ycqAppConfigUrl, int loginId) {
        if (appUrl.contains(SysmgrConstants.HTTP) || appUrl.contains(SysmgrConstants.HTTPS)) {
            // 如果url链接里带有http和https，就是外部系统，那么是否外部系统这个字段设置为1
            ycqAppConfigUrl.setMustExternal(SysmgrConstants.ONE_INT);
        } else {
            ycqAppConfigUrl.setMustExternal(SysmgrConstants.ZERO_INT);
        }
        ycqAppConfigUrl.setUpdateTime(new Date());
        ycqAppConfigUrl.setUpdateWorker(loginId);
    }

    public Result<Object> checkRepeatUpdateState(String logoUrl, String appName, String sort, String appId) {
        // 进行功能图标的数据重复校验,若值为0以上则返回页面提醒有重复数据
        int logoUrlCount = accessJdbcDao.checkRepeatUpdate(SysmgrConstants.LOGO_URL_FIELD, logoUrl, appId);
        if (logoUrlCount > 0) {
            return Result.ofFail("其他数据已有该功能图标,请重新输入");
        }
        // 进行功能名称的数据重复校验
        int appNameCount = accessJdbcDao.checkRepeatUpdate(SysmgrConstants.APP_NAME_FIELD, appName, appId);
        if (appNameCount > 0) {
            return Result.ofFail("其他数据已有该功能名称据,请重新填写");
        }
        // 进行排序数据重复校验
        int sortCount = accessJdbcDao.checkRepeatUpdate(SysmgrConstants.SORT, sort, appId);
        if (sortCount > 0) {
            return Result.ofFail("其他数据已有该排序数据,请重新填写");
        }
        return null;
    }
}
