package com.willalp.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.app.domain.HsAppHome;
import com.willalp.app.domain.HsAppHomeData;
import com.willalp.app.mapper.HsAppHomeMapper;
import com.willalp.app.service.IHsAppHomeDataService;
import com.willalp.app.service.IHsAppHomeService;
import com.willalp.common.core.domain.vo.SysDeptVo;
import com.willalp.common.core.redis.RedisCache;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.system.domain.resp.OrganizationListResp;
import com.willalp.system.service.ISysUserOrganizationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * app首页Service业务层处理
 *
 * @author willalp
 * @date 2022-01-11
 */
@Service
public class HsAppHomeServiceImpl extends ServiceImpl<HsAppHomeMapper, HsAppHome> implements IHsAppHomeService {
    @Resource
    private HsAppHomeMapper hsAppHomeMapper;

    @Resource
    private IHsAppHomeDataService hsAppHomeDataService;

    @Resource
    private ISysUserOrganizationService userOrganizationService;

    /**
     * 查询app首页列表
     *
     * @param hsAppHome app首页
     * @param userName
     * @return app首页
     */
    @Override
    public List<HsAppHome> selectHsAppHomeList(HsAppHome hsAppHome, String userName) {
        if ("admin".equals(userName)) {
            return hsAppHomeMapper.selectHsAppHomeList(hsAppHome);
        }
        List<OrganizationListResp> organizationByUserName = userOrganizationService.getOrganizationByUserName(userName);
        QueryWrapper<HsAppHome> qwUser = new QueryWrapper<>();
        organizationByUserName.forEach((o) -> {
            if (1 == o.getIsDefault()) {
                qwUser.eq("org_code", o.getOrganizationCode());
            }
        });
        return hsAppHomeMapper.selectList(qwUser);
        //return hsAppHomeMapper.selectHsAppHomeList(hsAppHome);
    }

    @Override
    public List<SysDeptVo> selectSysDeptList() {
        return hsAppHomeMapper.selectSysDeptList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveHomeProject(HsAppHome appHome, HsAppHomeData appHomeData) {
        //获取方案数据ID
        Long dataId = hsAppHomeDataService.saveHsAppHomeData(appHomeData);
        //保存进首页方案列表
        appHome.setHomeId(dataId);
        return save(appHome);
    }


    @Override
    public boolean issueHome(Long appHomeId) {
        if (ObjectUtil.isNull(appHomeId)) {
            throw new BaseException("app.home.config.appHomeId.null", new Object[]{appHomeId});
        }
        HsAppHome byId = this.getById(appHomeId);
        if (ObjectUtil.isNull(byId)) {
            throw new BaseException("app.home.config.homePojo.null", new Object[]{appHomeId});
        }
        byId.setIsIssue(1);
        return this.updateById(byId);
    }


    @Resource
    RedisCache redisCache;

    @Override
    public JSONObject getHomeConfigData(String orgCode)  {
        if (StringUtils.isNull(orgCode)) {
            throw new BaseException("app.home.config.orgCode.null");
        }
        JSONObject jsonObject = redisCache.getCacheObject(orgCode + "_HomeData");
        if (ObjectUtil.isEmpty(jsonObject)) {
            throw new BaseException("没有发布资讯");
        }
        return jsonObject;
    }
}
