package cn.com.countrygarden.bi.main.service.twerp.hm.impl;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbDesktopResourceRoomStateMapper;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopResourceRoomState;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbDesktopResourceRoomStateService;
import cn.com.countrygarden.bi.main.util.MD5;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
@Transactional
public class TbDesktopResourceRoomStateServiceImpl
        extends AbstractService<TbDesktopResourceRoomState>
        implements TbDesktopResourceRoomStateService {

    protected final Logger log = LoggerFactory.getLogger(TbDesktopResourceRoomStateServiceImpl.class);

    @Autowired
    private TbDesktopResourceRoomStateMapper mapper;


    /**
     * 获取用户资源数据
     *
     * @param userCode
     * @param organCode
     * @param commID
     * @return
     */
    public List<Map<String, String>> sumByUserCodeFromCache(String userCode, String organCode, String commID) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("userCode", userCode);
            put("organCode", organCode);
            put("commID", commID);
        }};
        Function<Map<String, String>, List<Map<String, String>>> function = p -> sumByUserCodeFromMapperToArrayList(p);

        List<Map<String, String>> resource = RedisCacheUtils.rememberList(
                "biapp:desktop:resource:room:state",
                1800 /* 15 * 60 */,
                param,
                function
        );

        return resource;
    }

    /**
     * 根据参数获取 resource 汇总
     *
     * @param param
     * @return
     */
    public List<TbDesktopResourceRoomState> sumByUserCodeFromMapper(Map<String, String> param) {
        List<TbDesktopResourceRoomState> state;
        String userCode = param.getOrDefault("userCode", "");
        String organCode = param.getOrDefault("organCode", "");
        String commID = param.getOrDefault("commID", "");

        if (userCode != null && !userCode.equals("")
                && organCode != null && !organCode.equals("")
                && commID != null && !commID.equals("")) {
            state = mapper.sumByUserCodeAndOrganCodeAndCommID(new HashMap<String, Object>() {{
                put("userCode", userCode);
                put("organCode", organCode);
                put("commID", commID);
            }});
        } else if (userCode != null && !userCode.equals("")
                && organCode != null && !organCode.equals("")) {
            state = mapper.sumByUserCodeAndOrganCode(new HashMap<String, Object>() {{
                put("userCode", userCode);
                put("organCode", organCode);
            }});
        } else if (userCode != null && !userCode.equals("")) {
            state = mapper.sumByUserCode(userCode);
        } else {
            state = null;
        }

        return state;
    }

    /**
     * 解析成 ArrayList 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private List<Map<String, String>> sumByUserCodeFromMapperToArrayList(Map<String, String> param) {
        List<Map<String, String>> data = new ArrayList<>();
        List<TbDesktopResourceRoomState> states = sumByUserCodeFromMapper(param);

        if (states == null) {
            return data;
        }

        for (TbDesktopResourceRoomState state : states) {
            data.add(new HashMap<String, String>(){{
                put("statename", state.getStateName());
                put("sumroomnum", state.getRoomNum());
                put("sumarea", state.getRoomBuildArea());
            }});
        }

        return data;
    }
}
