package ink.wgink.common.base;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.component.SecurityComponent;
import ink.wgink.common.enums.RoleDataRightEnum;
import ink.wgink.exceptions.AccessTokenException;
import ink.wgink.exceptions.AppTokenException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.app.AppTokenUser;
import ink.wgink.pojo.bos.UserInfoBO;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.util.ReflectUtil;
import ink.wgink.util.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @ClassName: AbstractService
 * @Description: baseinfo service
 * @Author: wenc
 * @Date: 2018/9/27 下午3:49
 * @Version: 1.0
 **/
@Component
public class DefaultBaseService {

    protected static final Logger LOG = LoggerFactory.getLogger(DefaultBaseService.class);
    @Autowired
    protected SecurityComponent securityComponent;
    @Autowired
    protected HttpSession httpSession;
    @Autowired
    protected HttpServletRequest httpServletRequest;
    @Autowired
    protected HttpServletResponse httpServletResponse;

    /**
     * 设置新增基础数据
     *
     * @param params
     */
    protected void setSaveInfo(Map<String, Object> params) {
        UserInfoBO userInfoBO = securityComponent.getCurrentUser();
        setSave(userInfoBO.getUserId(), params);
    }

    protected AppTokenUser getAppTokenUser(String token) {
        try {
            return securityComponent.getAppTokenUser(token);
        } catch (ReflectUtil.ReflectException e) {
            throw new AppTokenException("未引入APP的依赖");
        }
    }

    protected void setAppSaveInfo(String token, Map<String, Object> params) {
        AppTokenUser appTokenUser = getAppTokenUser(token);
        setSave(appTokenUser.getId(), params);
    }

    /**
     * 设置新增基础信息
     *
     * @param params
     * @param userId
     */
    protected void setSaveInfoByUserId(Map<String, Object> params, String userId) {
        setSave(userId, params);
    }

    /**
     * 设置新增
     *
     * @param userId
     * @param params
     */
    private void setSave(String userId, Map<String, Object> params) {
        String currentDate = DateUtil.getTime();
        params.put("creator", userId);
        params.put("gmtCreate", currentDate);
        params.put("modifier", userId);
        params.put("gmtModified", currentDate);
        params.put("isDelete", 0);
    }

    /**
     * 设置修改基础数据
     *
     * @param params
     */
    protected void setUpdateInfo(Map<String, Object> params) {
        UserInfoBO userInfoBO = securityComponent.getCurrentUser();
        if (userInfoBO != null) {
            setUpdate(userInfoBO.getUserId(), params);
        } else {
            setUpdate("1", params);
        }
    }

    protected void setAppUpdateInfo(String token, Map<String, Object> params) {
        AppTokenUser appTokenUser = getAppTokenUser(token);
        setUpdate(appTokenUser.getId(), params);
    }

    /**
     * 设置更新基础信息
     *
     * @param params
     * @param userId
     */
    protected void setUpdateInfoByUserId(Map<String, Object> params, String userId) {
        setUpdate(userId, params);
    }

    /**
     * 设置修改
     *
     * @param userId
     * @param params
     */
    private void setUpdate(String userId, Map<String, Object> params) {
        String currentDate = DateUtil.getTime();
        params.put("modifier", userId);
        params.put("gmtModified", currentDate);
    }

    /**
     * 设置zTree内容
     *
     * @param zTreeDTO
     * @param subCount
     */
    protected void setZTreeInfo(ZTreeDTO zTreeDTO, Integer subCount) {
        zTreeDTO.setUrl("javascript:void(0);");
        if (null != subCount && 0 < subCount) {
            zTreeDTO.setIsParent(true);
        } else {
            zTreeDTO.setIsParent(false);
        }
    }

    /**
     * 设置zTree内容
     *
     * @param zTreeDTO
     * @param subCount
     */
    protected void setZTreeInfo(ZTreeDTO zTreeDTO, long subCount) {
        zTreeDTO.setUrl("javascript:void(0);");
        if (subCount > 0) {
            zTreeDTO.setIsParent(true);
        } else {
            zTreeDTO.setIsParent(false);
        }
    }

    /**
     * 获取新的code
     *
     * @param code
     * @param parentCode
     * @return
     */
    protected String getNewCode(String code, String parentCode) {
        // 最后的code长度
        int lastCodeLength = code.length();
        int lastNumber = Integer.parseInt(code.substring(lastCodeLength - 4, lastCodeLength)) + 1;
        code = String.format("%04d", lastNumber);
        if (!StringUtils.isEmpty(parentCode)) {
            code = parentCode + code;
        }
        return code;
    }

    /**
     * 获取关键词
     *
     * @param params
     * @return
     */
    protected String getKeywords(Map<String, Object> params) {
        String keywords = null;
        if (params.get("keywords") != null) {
            keywords = params.get("keywords").toString();
        }
        params.remove("keywords");
        return keywords;
    }

    /**
     * 获取session
     *
     * @return
     */
    protected HttpSession getHttpSession() {
        return httpSession;
    }

    /**
     * 获取HashMap
     *
     * @return
     */
    protected Map<String, Object> getHashMap(int initSize) {
        return new HashMap<>(initSize);
    }

    /**
     * 是否管理员
     *
     * @return
     */
    protected boolean isAdmin() {
        return securityComponent.isAdmin();
    }

    /**
     * 获取基础部门ID列表
     *
     * @return
     */
    protected List<String> listBaseDepartmentIds() {
        if (isAdmin()) {
            return null;
        }
        return securityComponent.getCurrentUser().getRootDepartmentIds();
    }

    /**
     * 查询资源结果
     *
     * @param result       结果
     * @param errorMessage 异常提示
     * @throws AccessTokenException
     * @throws SearchException
     */
    protected void searchResourceResult(String result, String errorMessage) throws AccessTokenException, SearchException {
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException(errorMessage);
        }
    }

    /**
     * 更新结果
     *
     * @param result
     * @param errorMessage
     * @throws AccessTokenException
     * @throws SearchException
     */
    protected void updateResourceResult(String result, String errorMessage) throws AccessTokenException, SearchException {
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException(errorMessage);
        }
        JSONObject resultObj = JSONObject.parseObject(result);
        if (resultObj.getString("msg") != null) {
            throw new SearchException(resultObj.getString("msg"));
        }
    }

    /**
     * 简单Excel的Header
     *
     * @param names
     * @return
     */
    protected List<List<String>> simpleExcelHeader(String... names) {
        List<List<String>> listHeaders = new ArrayList<>();
        for (String name : names) {
            List<String> listHeader = new ArrayList<>();
            listHeader.add(name);
            listHeaders.add(listHeader);
        }
        return listHeaders;
    }

    /**
     * 设置数据权限信息
     *
     * @param params
     */
    protected void setDataAuthorityInfo(Map<String, Object> params) {
        UserInfoBO currentUser = securityComponent.getCurrentUser();
        if (securityComponent.isAdmin()) {
            return;
        }
        String dataAuthority = currentUser.getDataRight();
        List<String> dataAuthorityUserIds = currentUser.getDataUserIds();
        if (org.apache.commons.lang3.StringUtils.equals(RoleDataRightEnum.ALL.getValue(), dataAuthority)) {
            return;
        } else if (org.apache.commons.lang3.StringUtils.equals(RoleDataRightEnum.SELF.getValue(), dataAuthority)) {
            params.put(ISystemConstant.DATA_AUTHORITY, dataAuthority);
            params.put(ISystemConstant.DATA_CREATOR, currentUser.getUserId());
        } else if (org.apache.commons.lang3.StringUtils.equals(RoleDataRightEnum.DEPARTMENT.getValue(), dataAuthority)
                || org.apache.commons.lang3.StringUtils.equals(RoleDataRightEnum.CUSTOM.getValue(), dataAuthority)) {
            if (Objects.isNull(dataAuthorityUserIds) || dataAuthorityUserIds.isEmpty()) {
                LOG.debug("权限列表为空，设置查看个人");
                params.put(ISystemConstant.DATA_AUTHORITY, RoleDataRightEnum.SELF.getValue());
                params.put(ISystemConstant.DATA_CREATOR, currentUser.getUserId());
            } else {
                params.put(ISystemConstant.DATA_AUTHORITY, dataAuthority);
                params.put(ISystemConstant.DATA_CREATORS, dataAuthorityUserIds);
            }
        }
    }

}
