package com.gic.haoban.manage.service.service.out.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.UuidUtil;
import com.gic.haoban.manage.api.dto.ApplicationSettingDTO;
import com.gic.haoban.manage.api.service.ApplicationSettingApiService;
import com.gic.haoban.manage.service.dao.mapper.ApplicationSettingMapper;
import com.gic.haoban.manage.service.entity.TabHaobanApplicationSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by tgs on 2020/2/9.
 */
@Service
public class ApplicationSettingApiServiceImpl implements ApplicationSettingApiService {
    @Autowired
    private ApplicationSettingMapper applicationSettingMapper;

    @Override
    public void saveSetting(String wxEnterpriseId, JSONArray jsonArr) {
        for(int i=0;i<jsonArr.size();i++) {
            JSONObject json = jsonArr.getJSONObject(i) ;
            String applicationId = json.getString("applicationId") ;
            Integer openFlag = json.getInteger("openFlag") ;
            if(null == openFlag || null == applicationId) {
                continue ;
            }
            TabHaobanApplicationSetting tab = applicationSettingMapper.findSetting(wxEnterpriseId, applicationId);
            if (tab == null) {
                tab = new TabHaobanApplicationSetting();
                tab.setApplicationId(applicationId);
                tab.setCreateTime(new Date());
                tab.setOpenFlag(openFlag);
                tab.setSettingId(UuidUtil.randomUUID());
                tab.setStatusFlag(1);
                tab.setUpdateTime(new Date());
                tab.setWxEnterpriseId(wxEnterpriseId);
                applicationSettingMapper.insert(tab);
            } else {
                applicationSettingMapper.saveSetting(wxEnterpriseId, applicationId, openFlag);
            }
        }
    }

    @Override
    public ApplicationSettingDTO findSettingByWxEnterpriseIdAndApplicationId(String wxEnterpriseId, String applicationId) {
        TabHaobanApplicationSetting tab = applicationSettingMapper.findSetting(wxEnterpriseId, applicationId);
        if (tab == null) {
            return null;
        }
        return EntityUtil.changeEntity(ApplicationSettingDTO.class, tab);
    }

    @Override
    public List<ApplicationSettingDTO> listOpenByWxEnterpriseIdAndApplicationIds(String wxEnterpriseId, List<String> applicationIds) {
        if (applicationIds == null || applicationIds.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<TabHaobanApplicationSetting> list = applicationSettingMapper.listOpenByWxEnterpriseIdAndApplicationIds(wxEnterpriseId, applicationIds);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        List<ApplicationSettingDTO> result = EntityUtil.changeEntityListByJSON(ApplicationSettingDTO.class, list);
        return result;
    }

    @Override
    public List<String> findMenuCodes(String wxEnterpriseId,Integer openFlag) {
        return applicationSettingMapper.findMenuCodes(wxEnterpriseId,openFlag);
    }
}
